admin管理员组文章数量:1124406
I found the following generic type in some codebases:
type MyType<T> = {[K in keyof T]: T[K] }
Why is it not the same as type MyType<T> = T
?
I tried to find the difference in the types/values keyof MyType<SomeOtherType>
can have, but failed, are there other implications to the first Type definition?
/* eslint-disable @typescript-eslint/no-unused-vars */
type BaseType = {
a: number
b: string
}
type MyTypeA<T> = {
[K in keyof T]: T[K]
}
type MyTypeB<T> = T
function MyFuncA<T extends BaseType>(a: MyTypeA<T>, key: keyof T) {
const c = a[key]
return c
}
function MyFuncB<T extends BaseType>(a: MyTypeB<T>, key: keyof T) {
const c = a[key]
return c
}
type ExtendedType = BaseType & {
c: boolean // additional property
}
const extendedObject: ExtendedType = {
a: 1,
b: 'test',
c: true,
}
const CA = MyFuncA(extendedObject, 'a') // CA has type "number | sting | boolean"
const CB = MyFuncB(extendedObject, 'a') // CA also has type "number | sting | boolean"
本文标签: typescriptWhat is the diference between quotK in keyof T TK quot and just quotTquotStack Overflow
版权声明:本文标题:typescript - What is the diference between "{[K in keyof T]: T[K] }" and just "T" - Stack Ov 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1736628423a1945724.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论