























enum Type {
Title = 'Title',
Input = 'Input',
}
type InputConfType = number;
type TitleConfType = string
// map 1 对 1
type ConfList = {
[Type.Input]: InputConfType,
[Type.Title]: TitleConfType,
};
// 想生成一个接口 A, 确保 type 和 conf 获得对应类型, 适用于以下情况
const Input : A ={
type: Type.Input,
conf: 11
}
const Title : A ={
type: Type.Title,
conf: 'str'
}
// 如果出现有以下情况则报错
const ErrorTemplate : A ={
type: Type.Input,
conf: 'str
}
想象中的 A 接口 效果 类似于, 求解....
interface A < T=ConfList, K=keyof T > {
type: K;
conf: T[K]
}
Ts 想写完美, 特别是涉及 约束 真像是戴着镣铐跳舞....
as 一把梭又感觉不太优雅~~~
🐶
此内容由惯性聚合(RSS阅读器)自动聚合整理,仅供阅读参考。 原文来自 — 版权归原作者所有。