【发布时间】:2022-01-25 12:54:49
【问题描述】:
我有一个大致如下的对象:
const colors = {
gray: {
50: "#f9fafb",
100: "#f3f4f6",
},
red: {
50: "#fef2f2",
100: "#fee2e2",
200: "#fecaca",
}
}
我正在创建一个从 colors 对象返回颜色的辅助函数:
interface GetColor {
(selector: keyof typeof colors, tint: // ...?): // ...?
}
const getColor = (selector, tint) => {
return colors[selector][tint];
}
如何获得参数的正确类型(根据 selector 值推断 tint)和返回值?
【问题讨论】:
-
都是对象属性,所以都是字符串。
-
我知道,我的意思是,有没有一种方法可以根据
selector值而不只是类型编号来推断tint的类型为 (50 | 100 | etc...)
标签: typescript types