如何在 TypeScript 中创建数组的预定义可接受值?
How to create predefined acceptable values of array in TypeScript?
我在 TypeScript 中有这个对象:
validationRules: ValidationRuleInterface = {
id: ['integer', 'required'],
name: ['string', 'required', 'people_name'],
city: ['string', 'nullable'],
};
我想以某种方式在数组中定义可接受的值。像这样:
type Rule = string<'required' | 'nullable' | 'string' | 'integer' | 'people_name', null>;
interface ValidationRuleInterface {
[key: string]: Rule[]
}
有什么方法可以在 TypeScript 中定义数组的可能值吗?
你快到了。只需将类型定义更改为
type Rule = 'required' | 'nullable' | 'string' | 'integer' | 'people_name' | null;
我在 TypeScript 中有这个对象:
validationRules: ValidationRuleInterface = {
id: ['integer', 'required'],
name: ['string', 'required', 'people_name'],
city: ['string', 'nullable'],
};
我想以某种方式在数组中定义可接受的值。像这样:
type Rule = string<'required' | 'nullable' | 'string' | 'integer' | 'people_name', null>;
interface ValidationRuleInterface {
[key: string]: Rule[]
}
有什么方法可以在 TypeScript 中定义数组的可能值吗?
你快到了。只需将类型定义更改为
type Rule = 'required' | 'nullable' | 'string' | 'integer' | 'people_name' | null;