Skip to content

Instantly share code, notes, and snippets.

@songlairui
Created November 22, 2019 10:51
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save songlairui/52709baee619aaefdba5787f7f58a6d6 to your computer and use it in GitHub Desktop.
Save songlairui/52709baee619aaefdba5787f7f58a6d6 to your computer and use it in GitHub Desktop.
Typescript 用法探索
/**
* interface OOO { o: string }
* key in keyof OOO
* 等价于 key in 'o' | 'o'
* 等价于 key in 'o'
* 一定程度绕过直观上需要传入generic type的场景 function t<T,B>():<T<B>>{return {} as any}
*/
function geta<KEY extends string = 'data'>(aa: string) {
const b: { [key in KEY]: any } = {
[aa]: aa,
} as any;
return b;
}
const b: {
ddd: any;
} = geta<'ddd'>('ddd');
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment