-
-
Notifications
You must be signed in to change notification settings - Fork 428
feat(macro): make select macro choices strictly typed #2218
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -154,12 +154,21 @@ export function selectOrdinal( | |
| options: ChoiceOptions | ||
| ): string | ||
|
|
||
| type SelectOptions = { | ||
| type SelectOptionsExhaustive<T extends string = string> = { | ||
| [key in T]: string | ||
| } | ||
|
|
||
| type SelectOptionsNonExhaustive<T extends string = string> = { | ||
| /** Catch-all option */ | ||
| other: string | ||
| [matches: string]: string | ||
| } & { | ||
| [key in T]?: string | ||
| } | ||
|
Comment on lines
+164
to
166
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. can reuse the
but no strong feelings about this being better |
||
|
|
||
| type SelectOptions<T extends string = string> = | ||
| | SelectOptionsExhaustive<T> | ||
| | SelectOptionsNonExhaustive<T> | ||
|
|
||
| /** | ||
| * Selects a translation based on a value | ||
| * | ||
|
|
@@ -180,9 +189,9 @@ type SelectOptions = { | |
| * @param value The key of choices to use | ||
| * @param choices | ||
| */ | ||
| export function select( | ||
| value: string | LabeledExpression<string>, | ||
| choices: SelectOptions | ||
| export function select<T extends string = string>( | ||
| value: T | LabeledExpression<T>, | ||
| choices: SelectOptions<T> | ||
| ): string | ||
|
|
||
| /** | ||
|
|
||
| Original file line number | Diff line number | Diff line change | ||||||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
|
|
@@ -11,7 +11,8 @@ import { | |||||||||||||||
| import React from "react" | ||||||||||||||||
| import { ph } from "@lingui/core/macro" | ||||||||||||||||
|
|
||||||||||||||||
| const gender = "male" | ||||||||||||||||
| type Gender = "male" | "female" | ||||||||||||||||
| const gender = "male" as Gender | ||||||||||||||||
| const user = { | ||||||||||||||||
| name: "John", | ||||||||||||||||
| } | ||||||||||||||||
|
|
@@ -126,8 +127,20 @@ m = ( | |||||||||||||||
| // @ts-expect-error: `value` could be string only | ||||||||||||||||
| m = <Select value={5} other={"string"} /> | ||||||||||||||||
|
|
||||||||||||||||
| // @ts-expect-error: `other` required | ||||||||||||||||
| m = <Select value={"male"} /> | ||||||||||||||||
| // @ts-expect-error: `other` required unless exhaustive | ||||||||||||||||
| m = <Select value={gender} /> | ||||||||||||||||
|
|
||||||||||||||||
| // @ts-expect-error: `other` required unless exhaustive | ||||||||||||||||
| m = <Select value={gender} _male="..." /> | ||||||||||||||||
|
|
||||||||||||||||
| // @ts-expect-error: `other` required unless exhaustive | ||||||||||||||||
| m = <Select value={gender} _female="..." /> | ||||||||||||||||
|
|
||||||||||||||||
| // non-exhaustive okay if other is defined | ||||||||||||||||
| m = <Select value={gender} _female="..." other="..." /> | ||||||||||||||||
|
|
||||||||||||||||
| // exhaustive okay without other | ||||||||||||||||
| m = <Select value={gender} _male="..." _female="..." /> | ||||||||||||||||
|
|
||||||||||||||||
|
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
just adding two type tests |
||||||||||||||||
| // @ts-expect-error: `value` required | ||||||||||||||||
| m = <Select other={"male"} /> | ||||||||||||||||
|
|
||||||||||||||||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
oh, that means SWC plugins changes would also be required
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I see, I guess that's in a different repo 👀
Uh oh!
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Here's my attempt at implementing the same behavior on SWC side:
lingui/swc-plugin#157
Let me know if it's going totally in the wrong direction