Home > Software design >  give property a type when using object destructuring from a js file
give property a type when using object destructuring from a js file

Time:11-18

I am declaring an object apiClient like so:

const { apiClient } : any = useAuth();

Is there anyway that I can declare the type of apiClient so I can use intellisense?

I tried

const { apiClient } : { apiClient : JsonServiceClient } = useAuth();

But get error: Property 'apiClient' is missing in type '{}' but required in type '{ apiClient: JsonServiceClient; }'.ts(2741)

useAuth() is declared in a normal js file so it doesn't have a return type.

CodePudding user response:

Try

const { apiClient } = useAuth() as { apiClient : JsonServiceClient };
  • Related