In my Next.js web app, I’m importing my Swagger/OpenAPI types into global.d.ts
with:
type ApiDefinitions = import('types/swagger').definitions
…and then I can simplify the use of that type in my app with:
type User = ApiDefinitions.User
But what if I want to use interface User
instead of type User
?
This doesn’t work because it seems to define User
as any
:
interface User extends ApiDefinitions.User {}
(Produces error: Type error: Property 'email' does not exist on type 'User'
)
CodePudding user response:
you can try to keep using types:
type User = ApiDefinitions.User & {
customField: unknown;
}
CodePudding user response:
If you want the User interface as an actual interface, that's "importing" the User interface from the namespace ApiDefinitions, no?
Similar to how we can use import("module")
to import the module's types as a namespace, we can use import
to also import a type from a namespace:
import User = ApiDefinitions.User;
Wack syntax but it is reasonable. This is kind of like use
/using
in most languages.
You might also want:
import ApiDefinitions = import("types/swagger").definitions;
instead.