Skip to content

Commit

Permalink
Merge pull request #324 from jpudysz/feature/flatten
Browse files Browse the repository at this point in the history
feat: flatten variants in case of never
  • Loading branch information
jpudysz authored Oct 26, 2024
2 parents c18e736 + c567e77 commit ecdb335
Show file tree
Hide file tree
Showing 3 changed files with 16 additions and 29 deletions.
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

0 comments on commit ecdb335

Please sign in to comment.