Implement proper type mapping for importLibrary() #95
Labels
triage me
I really want to be triaged.
type: feature request
‘Nice-to-have’ improvement, new feature or different behavior or design.
Problem
The definition of the
importLibrary
function returns a union-type of all the possible libraries, which means that users will always need to explicitly type-cast the results to get a proper type for the library. In JS these type-casts aren't really an option, so JS users don't get proper IDE support at all. Also, with the parameter being typed as "string", there's no validation for correct library names.js-types/index.d.ts
Lines 7358 to 7373 in 32725e7
Possible solution
Add explicit typing for the
importLibrary
function, such that the returned library type is inferred from the parameter.This will also automatically add validation for the library name parameter.
A good way to do this would be (typescript playground link):
This is the pattern that is used in the typescript DOM library for e.g. element types (for example to determine the correct return type of
document.createElement
).By using an Interface to define the mapping from library name to implementation, the library declarations can be done throughout the .d.ts file and will use declaration-merging to create the final
ApiLibraryMap
.This might also help with your generated structure here, since the individual libraries can be specified where they are defined. For example:
The text was updated successfully, but these errors were encountered: