0

I have this code:

cellRendererFramework: (params: any) => {
    return (<>{params.data.SOME_PROPERTY_FROM_USER}</>);
},

params is an object. And I am accessing only params.data all the time. I would like to define params.data with typescript.

This is a working example:

cellRendererFramework: (params: any) => {
    const userdata: User = params.data;
    return (<>{userdata.SOME_PROPERTY_FROM_USER}</>);
},

Now sonarLint is telling me all the time I should remove the useless declaration of userdata. Understandable, because I am using userdata only to check the data structure of the user is correct.

In my perfect world I would like to write this:

cellRendererFramework: (params.data: User) => {
    return (<>{params.data.SOME_PROPERTY_FROM_USER}</>);
},

But this is not allowed. Is there any good solution for this problem? Thanks

Paul
  • 1,344
  • 4
  • 19
  • 37
  • 1
    Does this answer your question? [Typed function parameters using destructuring and rest in TypeScript](https://stackoverflow.com/questions/53329592/typed-function-parameters-using-destructuring-and-rest-in-typescript) – Emile Bergeron Aug 14 '20 at 16:59

1 Answers1

3

You can use an object desctructuring assignment with typing added like this:

cellRendererFramework: ({ data }: { data: User }) => {
    return (<>{params.data.SOME_PROPERTY_FROM_USER}</>);
},

It is also explained in more detail here.

David Losert
  • 4,652
  • 1
  • 25
  • 31