I have a function that accepts some data and function add some properties and return that parameter object.
this is how I am declaring it
async setContext(@Body() data: { [key: string]: any }): Promise<{
additionalData: { id: string, [key: string]: any },
context: Context,
[key: string]: any
}> {
data.additionalData.id = this._getId(data.additionalData);
data.context = this._getContext();
return data;
}
but this is showing
Type '{ [key: string]: any; }' is missing the following properties from type '{ [key: string]: any; additionalData: { [key: string]: any; id: string; }; context: Context; }': additionalData, contextts(2739)
so I tried
async setContext(@Body() data: { additionalData?: any, context?: Context, [key: string]: any }): Promise<{
additionalData: { id: string, [key: string]: any },
context: Context,
[key: string]: any
}> {
if (!data.additionalData) {
data.additionalData = {};
}
data.additionalData.id = this._getConversationId(data.additionalData);
data.context = this._getContext();
return data;
}
but now this is showing
Type '{ [key: string]: any; additionalData?: any; context?: Context; }' is not assignable to type '{ [key: string]: any; additionalData: { [key: string]: any; id: string; }; context: Context; }'.
Property 'additionalData' is optional in type '{ [key: string]: any; additionalData?: any; context?: Context; }' but required in type '{ [key: string]: any; additionalData: { [key: string]: any; id: string; }; context: Context; }'.ts(2322)
should not this work as I am declaring data as
data: { [key: string]: any }
to have any unknown properties?
question from:
https://stackoverflow.com/questions/65914835/object-with-some-specific-properties-typescript 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…