Skip to content
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

feat(core/i18n/t): Precise typing for message parameters #1840

Closed
wants to merge 24 commits into from
Closed
Show file tree
Hide file tree
Changes from 1 commit
Commits
Show all changes
24 commits
Select commit Hold shift + click to select a range
a89baef
feat(core/i18n/t): Precise typing for message parameters
dilame Jan 22, 2024
865dfad
feat(core/i18n/t): take into account escape characters
dilame Jan 22, 2024
2da35cf
feat(core/i18n/t): only allow omit values param object if message str…
dilame Jan 22, 2024
61e92d4
chore(core/i18n/t): use type-fest lib instead of manually declared ut…
dilame Jan 22, 2024
1777509
fix(core/i18n/t): trim interpolation parameter names
dilame Jan 22, 2024
ab1419f
refactor: message descriptor types for core and macro
dilame Jan 23, 2024
7f9e45d
test: add tests for new strict string interpolation typings
dilame Jan 23, 2024
fdd3fd8
refactor: rename type I18nT to I18nTValues
dilame Jan 23, 2024
01fa074
chore: yarn.lock type-fest
dilame Jan 23, 2024
a53d429
refactor: rename descriptor types
dilame Jan 23, 2024
1bf144f
chore: remove unnecessary eslint-ignore
dilame Jan 23, 2024
43edfb2
refactor: rename type I18nT to I18nTValues
dilame Jan 23, 2024
1726d7c
refactor: rename type _ExtractVars to ExtractVars
dilame Jan 23, 2024
a866f4f
refactor(core/i18n.t): use wide record type for values in case of wid…
dilame Jan 23, 2024
60bbaf8
test(core/i18n.t): make the most complex test case even more complex
dilame Jan 23, 2024
dfd9d8e
feat(core/i18n.t/values): add formatters strict typing support
dilame Jan 23, 2024
01d6608
test(core/i18n.t): formatter typings
dilame Jan 23, 2024
64c697b
fix(core/i18n.t/values): replace all escaped symbols instead of just …
dilame Jan 23, 2024
53d904b
test(core/i18n.t/values): ensure all escaped symbols in string are dr…
dilame Jan 23, 2024
9d29f2d
chore(core/i18n.t/values): remove unnecessary condition in type Extra…
dilame Jan 23, 2024
c8cf4d1
chore(core/i18n.t/values): write explanation comments at each line of…
dilame Jan 23, 2024
e42c01e
chore(core/i18n.t/values): write explanation comments at each line of…
dilame Jan 23, 2024
d8463d1
chore(core/i18n/t): combine excessive overloads into one signature
dilame Jan 26, 2024
3fc4562
chore(core/i18n/t): rename parameter 'id' to '_' in overloads
dilame Jan 26, 2024
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions packages/core/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,7 @@
"dependencies": {
"@babel/runtime": "^7.20.13",
"@lingui/message-utils": "4.7.0",
"type-fest": "^4.9.0",
"unraw": "^3.0.0"
},
"devDependencies": {
Expand Down
19 changes: 5 additions & 14 deletions packages/core/src/i18n.t.ts
Original file line number Diff line number Diff line change
@@ -1,14 +1,10 @@
// Already listed, not sure what else to do
// eslint-disable-next-line import/no-extraneous-dependencies
import { Replace, Simplify, Trim, UnionToIntersection } from "type-fest"
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

if you use webstorm and see warning from eslint from this rule that's probable because webstorm's eslint service should be restarted. It doesn't pick up changes in package.json automatically and continues to show error even if the package is already added. However this is not the case when you run eslint command directly.

import { Formats } from "./i18n"

export type Replace<
Input extends string,
Search extends string,
Replacement extends string,
> = Input extends `${infer Head}${Search}${infer Tail}`
? `${Head}${Replacement}${Replace<Tail, Search, Replacement>}`
: Input;

type DropEscapedBraces<Input extends string> = Replace<Input, `'{` | `}'`, ''>;
type DropEscapedBraces<Input extends string> = Replace<Replace<Input, `'{`, ''>, `}'`, ''>;

type ExtractNextBrace<T extends string, Acc extends string = ""> = T extends `${infer Head}${infer Tail}` ?
Head extends "{" | "}" ? [Acc, Head, Tail] : ExtractNextBrace<Tail, `${Acc}${Head}`>
Expand Down Expand Up @@ -58,11 +54,6 @@ type ExtractFormatterMessages<Input extends string> =
[]
;

type UnionToIntersection<U> =
(U extends any ? (x: U) => void : never) extends ((x: infer I) => void) ? I : never

type Normalize<T> = { [K in keyof T]: T[K] } & {}

type _ExtractVars<Input extends string> =
string extends Input
?
Expand All @@ -84,7 +75,7 @@ type _ExtractVars<Input extends string> =
: {}
;

export type I18nT<Input extends string> = Normalize<_ExtractVars<DropEscapedBraces<Input>>>;
export type I18nT<Input extends string> = Simplify<_ExtractVars<DropEscapedBraces<Input>>>;

type MessageDescriptorWithIdAsMessage<Message extends string> =
{} extends I18nT<Message>
Expand Down
9 changes: 9 additions & 0 deletions yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -3299,6 +3299,7 @@ __metadata:
"@babel/runtime": ^7.20.13
"@lingui/jest-mocks": "*"
"@lingui/message-utils": 4.7.0
type-fest: ^4.9.0
unbuild: 2.0.0
unraw: ^3.0.0
languageName: unknown
Expand Down Expand Up @@ -10678,6 +10679,7 @@ __metadata:
strip-ansi: ^6.0.1
swc-node: ^1.0.0
ts-jest: ^29.0.5
type-fest: ^4.9.0
typescript: ^4.9.5
languageName: unknown
linkType: soft
Expand Down Expand Up @@ -15125,6 +15127,13 @@ __metadata:
languageName: node
linkType: hard

"type-fest@npm:^4.9.0":
version: 4.9.0
resolution: "type-fest@npm:4.9.0"
checksum: 73383de23237b399a70397a53101152548846d919aebcc7d8733000c6c354dc2632fe37c4a70b8571b79fdbfa099e2d8304c5ac56b3254780acff93e4c7a797f
languageName: node
linkType: hard

"typed-array-length@npm:^1.0.4":
version: 1.0.4
resolution: "typed-array-length@npm:1.0.4"
Expand Down