如下所示
export const tuple = <T extends string[]>(...args: T) => args;
const ButtonTypes = tuple('default', 'primary', 'ghost', 'dashed', 'link', 'text');
export type ButtonType = typeof ButtonTypes[number];
你知道ButtonType
的最终类型是什么吗?
正确类型为
type ButtonType = "default" | "primary" | "ghost" | "dashed" | "link" | "text";
为什么会是这个结果?首先可以推断出ButtonTypes
的类型是["default", "primary", "ghost", "dashed", "link", "text"]
。然后,使用["default", "primary", "ghost", "dashed", "link", "text"][number]
将会检索这个类型数组中的所有属性,使用数字索引对其访问,因此会出现上述所示的类型结果。
参考自:https://stackoverflow.com/questions/56897267/what-does-typeof-tuple-with-index-type-signature-represents?noredirect=1&lq=1
如果觉得我的文章对您有用,请您随意打赏。您的支持将鼓励我更加努力创作!
如无特殊声明,文章均为原创,若有不正之处,万望告知。转载请附上原文地址,十分感谢!