Last active
November 23, 2018 23:25
-
-
Save lumie1337/e638f09cde2e9f1fb8a06398f9ad7796 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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" | |
} | |
})) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
type Many = 'foo' | 'bar' | 'baz'