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: flatten variants in case of never #324

Merged
merged 1 commit into from
Oct 26, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
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
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -91,7 +91,7 @@
"react-native-web": "0.19.12",
"react-test-renderer": "18.3.1",
"release-it": "17.4.0",
"typescript": "5.3.3"
"typescript": "5.6.3"
},
"peerDependencies": {
"@react-native/normalize-colors": "*",
Expand Down
21 changes: 14 additions & 7 deletions src/types/breakpoints.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,26 +18,33 @@ type ExtractBreakpoints<T> = T extends object
}
: T

type ParseNestedObject<T> = T extends (...args: infer A) => infer R
? (...args: A) => ParseNestedObject<R>
type ParseNestedObject<T, ShouldFlatten> = T extends (...args: infer A) => infer R
? (...args: A) => ParseNestedObject<R, false>
: T extends object
? T extends { variants: infer R }
? ParseVariants<FlattenVariants<R>> & ParseNestedObject<Omit<T, 'variants'>>
// if intersection of Base and Variants is never, then flatten variants to generic "string"
? (ParseVariants<FlattenVariants<R, false>> & ParseNestedObject<Omit<T, 'variants'>, false>) extends never
? ParseVariants<FlattenVariants<R, true>> & ParseNestedObject<Omit<T, 'variants'>, false>
: ParseVariants<FlattenVariants<R, false>> & ParseNestedObject<Omit<T, 'variants'>, false>
: {
[K in keyof T]: T[K] extends object
? T[K] extends OpaqueColorValue
? ColorValue
: ExtractBreakpoints<T[K]>
: T[K]
: T[K] extends string
? ShouldFlatten extends true
? string
: T[K]
: T[K]
}
: T

type FlattenVariants<T> = T extends object
type FlattenVariants<T, ShouldFlatten> = T extends object
? {
[K in keyof T]: T[K] extends object
? {
[key in keyof T[K]]: T[K][key] extends object
? ParseNestedObject<T[K][key]>
? ParseNestedObject<T[K][key], ShouldFlatten>
: never
}
: never
Expand All @@ -54,7 +61,7 @@ type UnionToIntersection<U> =
(U extends any ? (k: U) => void : never) extends ((k: infer I) => void) ? I : never

type ParseStyleKeys<T> = T extends object
? { [K in keyof T]: ParseNestedObject<T[K]> }
? { [K in keyof T]: ParseNestedObject<T[K], false> }
: never

export type ReactNativeStyleSheet<T> = T extends (theme: UnistylesTheme, runtime: UnistylesMiniRuntime) => infer R
Expand Down
22 changes: 1 addition & 21 deletions yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -20883,7 +20883,7 @@ __metadata:
react-native-web: 0.19.12
react-test-renderer: 18.3.1
release-it: 17.4.0
typescript: 5.3.3
typescript: 5.6.3
peerDependencies:
"@react-native/normalize-colors": "*"
react: "*"
Expand Down Expand Up @@ -23868,16 +23868,6 @@ __metadata:
languageName: node
linkType: hard

"typescript@npm:5.3.3":
version: 5.3.3
resolution: "typescript@npm:5.3.3"
bin:
tsc: bin/tsc
tsserver: bin/tsserver
checksum: 2007ccb6e51bbbf6fde0a78099efe04dc1c3dfbdff04ca3b6a8bc717991862b39fd6126c0c3ebf2d2d98ac5e960bcaa873826bb2bb241f14277034148f41f6a2
languageName: node
linkType: hard

"typescript@npm:5.4.5":
version: 5.4.5
resolution: "typescript@npm:5.4.5"
Expand Down Expand Up @@ -23908,16 +23898,6 @@ __metadata:
languageName: node
linkType: hard

"typescript@patch:[email protected]#~builtin<compat/typescript>":
version: 5.3.3
resolution: "typescript@patch:typescript@npm%3A5.3.3#~builtin<compat/typescript>::version=5.3.3&hash=29ae49"
bin:
tsc: bin/tsc
tsserver: bin/tsserver
checksum: f61375590b3162599f0f0d5b8737877ac0a7bc52761dbb585d67e7b8753a3a4c42d9a554c4cc929f591ffcf3a2b0602f65ae3ce74714fd5652623a816862b610
languageName: node
linkType: hard

"typescript@patch:[email protected]#~builtin<compat/typescript>":
version: 5.4.5
resolution: "typescript@patch:typescript@npm%3A5.4.5#~builtin<compat/typescript>::version=5.4.5&hash=29ae49"
Expand Down
Loading