Typescript条件动态模型类型声明
typescript是否有类似于动态类型声明的功能,根据值变量类型,它将分配适当的自定义模型Typescript条件动态模型类型声明,typescript,Typescript,typescript是否有类似于动态类型声明的功能,根据值变量类型,它将分配适当的自定义模型 export type ModelDeclaration = { [T: string]: typeof ModelDeclaration[T] === 'string' ? ModelStringInput : ModelIntInput; } export type ModelIntInput = { between?: Array<number | null> | null
export type ModelDeclaration = {
[T: string]: typeof ModelDeclaration[T] === 'string' ? ModelStringInput : ModelIntInput;
}
export type ModelIntInput = {
between?: Array<number | null> | null;
attributeExists?: boolean | null;
attributeType?: ModelAttributeTypes | null;
};
export type ModelStringInput = {
attributeType?: ModelAttributeTypes | null;
size?: ModelSizeInput | null;
};
导出类型模型声明={
[T:string]:ModelDeclaration的类型[T]=“string”?ModelStringInput:ModelIntInput;
}
导出类型ModelIntInput={
介于?之间:数组|空;
attributeExists?:布尔值| null;
attributeType?:ModelAttributeType | null;
};
导出类型ModelStringInput={
attributeType?:ModelAttributeType | null;
大小?:ModelSizeInput |空;
};
是的,这些都存在!然而,它们在某些方面有一定的局限性,在我看来,手册对此进行了很好的解释:
然而,您的示例看起来有点像您可以编写一个函数,将其包装成是的,它们是存在的!然而,它们在某些方面有一定的局限性,在我看来,手册对此进行了很好的解释:
但是,您的示例看起来有点像您可以编写一个函数,将其包装成您可以使用一个变量来区分两个选项:
export type ModelDeclaration =
{
value: string
type: 'string'
model: ModelStringInput
} |
{
value: number
type: 'int'
model: ModelIntInput
}
export type ModelIntInput = {
between?: Array<number | null> | null;
attributeExists?: boolean | null;
attributeType?: ModelAttributeTypes | null;
};
export type ModelStringInput = {
attributeType?: ModelAttributeTypes | null;
size?: ModelSizeInput | null;
};
const parseModel = (model: ModelDeclaration) => {
if (model.type === 'string') {
// Here typescript knows that model.model is ModelStringInput and model.value is string
}
if (model.type === 'int') {
// Here typescript knows that model.model is ModelIntInput and model.value is number
}
}
导出类型模型声明=
{
值:字符串
键入:“字符串”
模型:ModelStringInput
} |
{
值:数字
键入:“int”
型号:ModelIntInput
}
导出类型ModelIntInput={
介于?之间:数组|空;
attributeExists?:布尔值| null;
attributeType?:ModelAttributeType | null;
};
导出类型ModelStringInput={
attributeType?:ModelAttributeType | null;
大小?:ModelSizeInput |空;
};
const parseModel=(模型:ModelDeclaration)=>{
如果(model.type=='string'){
//这里typescript知道model.model是ModelStringInput,model.value是string
}
如果(model.type=='int'){
//这里typescript知道model.model是modelinInput,model.value是number
}
}
您可以使用一个变量来区分两个选项:
export type ModelDeclaration =
{
value: string
type: 'string'
model: ModelStringInput
} |
{
value: number
type: 'int'
model: ModelIntInput
}
export type ModelIntInput = {
between?: Array<number | null> | null;
attributeExists?: boolean | null;
attributeType?: ModelAttributeTypes | null;
};
export type ModelStringInput = {
attributeType?: ModelAttributeTypes | null;
size?: ModelSizeInput | null;
};
const parseModel = (model: ModelDeclaration) => {
if (model.type === 'string') {
// Here typescript knows that model.model is ModelStringInput and model.value is string
}
if (model.type === 'int') {
// Here typescript knows that model.model is ModelIntInput and model.value is number
}
}
导出类型模型声明=
{
值:字符串
键入:“字符串”
模型:ModelStringInput
} |
{
值:数字
键入:“int”
型号:ModelIntInput
}
导出类型ModelIntInput={
介于?之间:数组|空;
attributeExists?:布尔值| null;
attributeType?:ModelAttributeType | null;
};
导出类型ModelStringInput={
attributeType?:ModelAttributeType | null;
大小?:ModelSizeInput |空;
};
const parseModel=(模型:ModelDeclaration)=>{
如果(model.type=='string'){
//这里typescript知道model.model是ModelStringInput,model.value是string
}
如果(model.type=='int'){
//这里typescript知道model.model是modelinInput,model.value是number
}
}