Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
698 views
in Technique[技术] by (71.8m points)

object with some specific properties typescript

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

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

I would define the returned type of the function depending on the type of parameters of the function.


Playground


To use it in your case, just replace 'foo' and 'bar' by your functions.


type Context = string;

type DataIn = { 
  additionalData?: unknown;
  context?: Context;
  [key: string]: unknown;
};

type DataOut = Omit<Required<DataIn>, 'additionalData'> & {
  additionalData: { 
    id: string;
    [key: string]: unknown;
  };
};

async function setContext(data: DataIn): Promise<DataOut> {
  return {
    ...data,

    additionalData: {
      ...(data.additionalData as object),

      id: 'foo',
    },

    context: 'bar',
  };
}

(async() => {
  const myData = await setContext({
    animal: 'dog',
  });

  console.log(myData);
})();

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...