Created
April 26, 2018 10:14
-
-
Save kurogelee/8d4886833f4fdafef7bdcfb5add11cec to your computer and use it in GitHub Desktop.
Mapped typeとConditional typeを使ってclass/interfaceのプロパティの型を変更するサンプル ref: https://qiita.com/kurogelee/items/a6955d2f399d3929ef03
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
{ | |
id: string; | |
id2?: number; | |
key: string; | |
value?: boolean; | |
names?: Array<string>; | |
names2: Array<string>; | |
value2?: Array<Array<string>>; | |
} |
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 OrFunction<T> = T | ((val: string) => T); | |
interface Content { | |
id: string; | |
id2?: number; | |
key: OrFunction<string>; | |
value?: OrFunction<boolean>; | |
names?: Array<OrFunction<string>>; | |
names2: Array<OrFunction<string>>; | |
value2?: Array<Array<OrFunction<string>>>; | |
} |
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 Plain<T> = { [P in keyof T]: ExcludeArray<T[P], (val: any) => any> }; | |
type ExcludeArray<T, U> = T extends Array<infer R> ? Array<ExcludeArray2<R, U>> : Exclude<T, U>; | |
type ExcludeArray2<T, U> = T extends Array<infer R> ? Array<Exclude<R, U>> : Exclude<T, U>; | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment