TypeScript-DOM-lib-generator
TypeScript-DOM-lib-generator copied to clipboard
DOMString and friends are too narrow
DOMString
, USVString
, CSSOMString
and ByteString
are currently mapped to simply string
:
https://github.com/microsoft/TypeScript-DOM-lib-generator/blob/1fd97d19c109bc6ec2780605709f6e819356e942/src/helpers.ts#L53
However it doesn't include objects that can be stringified using the ToString
abstract operation.
For example, the following code is valid in JavaScript but doesn't compile in TypeScript:
new URL(window.location); // Error: Argument of type 'Location' is not assignable to parameter of type 'string'.
So I think this is a more general case of #331.
A possible solution would be to map them to string | { toString(): string }
instead, but that would pollute many DOM APIs. For example, it would make even the following code to compile without errors, which is not necessarily desired:
new URL({ toString() { return "hello"; } });
A restriction that can be made is allowing only interfaces with stringifiers (URL
is among them). From my understanding there are not so many such interfaces, so this wouldn't be much wider than string
.
Funny enough, my coworker @code-asher and I were just discussing this — specifically the new URL(window.location);
example.
We were wondering if the type definition for new URL
should be modified to accept Location
but it sounds like that is only a partial solution.
We're using new URL(window.location.toString());
as a current workaround.