Skip to content

Instantly share code, notes, and snippets.

@lumie1337
Last active November 23, 2018 23:25
Show Gist options
  • Save lumie1337/e638f09cde2e9f1fb8a06398f9ad7796 to your computer and use it in GitHub Desktop.
Save lumie1337/e638f09cde2e9f1fb8a06398f9ad7796 to your computer and use it in GitHub Desktop.
type CSSProperty = {
width: string
height: string
}
// Placeholder
type Theme = string
type StylesheetGroup = {
[s: string]: CSSProperty
}
type StylesheetSchema = ((...args: any[]) => StylesheetGroup) | StylesheetGroup
type ValidStylesheetSchema<T> = ({
[K1 in keyof T]: (T[K1] extends CSSProperty ? true : false) | {
[K2 in keyof T[K1]]: K2 extends keyof CSSProperty ? true : false
}[keyof T[K1]]
}[keyof T])
type StylesheetValidator<T extends StylesheetSchema> =
T extends (...args: any[]) => infer P ? (ValidStylesheetSchema<P> extends true ? T : "not a valid stylesheet") :
T extends StylesheetSchema ? (ValidStylesheetSchema<T> extends true ? T : "not a valid stylesheet") :
"not a valid stylesheet"
function stylesheet<T extends StylesheetValidator<P>, P extends StylesheetSchema = T>(value: T): T {
return value;
}
const styles = stylesheet((theme: Theme) => ({
a: {
width: "12",
height: "14"
}
}))
const styles2 = stylesheet({
a: {
width: "12",
height: "14"
}
}))
@kasper573
Copy link

type Many = 'foo' | 'bar' | 'baz'

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment