Created
November 22, 2019 10:51
-
-
Save songlairui/52709baee619aaefdba5787f7f58a6d6 to your computer and use it in GitHub Desktop.
Typescript 用法探索
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
/** | |
* 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