Scarlet
Scarlet

Reputation: 1

Typescript definition file - for a function, return actual property types, or custom property type itself?

I'm trying to write a TypeScript type definitions file for an external JS library, MusicKit JS. I'm fairly new to TS (and especially declaring type definitions in TS). The MusicKit docs are, at times, not very descriptive, and I'd like to avoid inheriting this in the type definitions.

As a result, I'm unsure of what return type to use on a function definition.

For example, take the authorize function. In the MusicKit docs, it is defined as such: Link to relevant docs section

Promise authorize();

where the return type is a Promise (containing a musicUserToken, although not specified).


In my type definitions, I define it as so:

authorize(): Promise<MusicKitInstance.musicUserToken>;

where the musicUserToken is defined

musicUserToken: string | undefined;

and the MusicKitInstance is a property of the MusicKit object.

To get this to work, I had to declare another module:

declare module MusicKitInstance {
    export type musicUserToken = string | undefined;
    ...
}

as I want to be clear on what is returned, a musicUserToken. But should I just ditch the type definition and return string | undefined directly? If I used the appropriate custom types for all the functions in the library, I would have to manually define all the types listed under the MusicKitInstance in the MusicKitInstance module, and then use them in the MusicKit module definitions. I'm not sure if this is a good practice.

Upvotes: 0

Views: 53

Answers (0)

Related Questions