Skip to content

Instantly share code, notes, and snippets.

@kurogelee
Created April 26, 2018 10:14
Show Gist options
  • Save kurogelee/8d4886833f4fdafef7bdcfb5add11cec to your computer and use it in GitHub Desktop.
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
{
id: string;
id2?: number;
key: string;
value?: boolean;
names?: Array<string>;
names2: Array<string>;
value2?: Array<Array<string>>;
}
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>>>;
}
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