如何在打字稿中动态设置对象键
How to dynamically set an object key in typescript
如何创建一个函数,其中对象的键是从函数动态设置的
export const validateObjectId = (key: string = 'id'): ObjectSchema => {
return Joi.object({
key: Joi.string()
.regex(/^(?=[a-f\d]{24}$)(\d+[a-f]|[a-f]+\d)/i)
.required(),
});
};
如何让key成为对象的key
也许您可以尝试以下方法:
export const validateObjectId = (key: string = 'id'): ObjectSchema => {
let object: any = {};
object[key] = Joi.string()
.regex(/^(?=[a-f\d]{24}$)(\d+[a-f]|[a-f]+\d)/i)
.required();
return Joi.object(object);
}
干杯
我已经能够使用下面的代码片段找到出路
export const validateObjectId = (key: string = 'id'): ObjectSchema => {
interface Obj {
[key: string]: Object;
}
const object: Obj = {};
object[key] = Joi.string()
.regex(/^(?=[a-f\d]{24}$)(\d+[a-f]|[a-f]+\d)/i)
.required();
return Joi.object(object);
};
如何创建一个函数,其中对象的键是从函数动态设置的
export const validateObjectId = (key: string = 'id'): ObjectSchema => {
return Joi.object({
key: Joi.string()
.regex(/^(?=[a-f\d]{24}$)(\d+[a-f]|[a-f]+\d)/i)
.required(),
});
};
如何让key成为对象的key
也许您可以尝试以下方法:
export const validateObjectId = (key: string = 'id'): ObjectSchema => {
let object: any = {};
object[key] = Joi.string()
.regex(/^(?=[a-f\d]{24}$)(\d+[a-f]|[a-f]+\d)/i)
.required();
return Joi.object(object);
}
干杯
我已经能够使用下面的代码片段找到出路
export const validateObjectId = (key: string = 'id'): ObjectSchema => {
interface Obj {
[key: string]: Object;
}
const object: Obj = {};
object[key] = Joi.string()
.regex(/^(?=[a-f\d]{24}$)(\d+[a-f]|[a-f]+\d)/i)
.required();
return Joi.object(object);
};