You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
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:
newURL(window.location);// Error: Argument of type 'Location' is not assignable to parameter of type 'string'.
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:
newURL({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.
It's cool that it picks up the [LegacyNullToEmptyString] annotation on value but it's a pain that it doesn't support the string coercion from the spec.
Specifically it's a pain that element.style.setProperty('x', 3.7); is rejected by TS when it's perfectly valid according to the spec.
If we look at the DOMString definition:
A JavaScript value V is converted to an IDL DOMString value by running the following algorithm:
Steps 1-8 are easy to support by making DOMString map to:
type DOMStringInput = string | undefined | null | boolean | number | BigInt
Note that this type is DOMStringInput not DOMString because the conversion only occurs on the JS -> DOMString side, and the type should not be used for IDL methods that returnDOMString
Steps 10-12 are tricky because it's not as simple as checking for a toString(): string method (as suggested above) because it can go unused if toPrmitive is defined (and if it is defined, we have no good way of knowing
type ToStringable = { toString(): string; [Symbol.toPrimitive]?: (hint: string) => DOMStringInput } // Slight approximation, the spec just says "Not an Object"
type DOMStringInput = string | undefined | null | boolean | number | BigInt | ToStringable
Which is getting a bit convoluted. Anyway it's unclear if this change, which would allow more programs, would encourage correct programs, and should be supported or not, but some guidance from the project would be cool here.
Activity
[-]DOMString and friends are not handled properly[/-][+]DOMString and friends are too narrow[/+]jsjoeio commentedon Nov 17, 2021
Funny enough, my coworker Asher (@code-asher) and I were just discussing this — specifically the
new URL(window.location);example.We were wondering if the type definition for
new URLshould be modified to acceptLocationbut it sounds like that is only a partial solution.We're using
new URL(window.location.toString());as a current workaround.djrenren commentedon Sep 4, 2025
Ran into this with
CSSStyleDeclaration.setPropertywhich has the following type intypescript:It's cool that it picks up the [LegacyNullToEmptyString] annotation on
valuebut it's a pain that it doesn't support the string coercion from the spec.Specifically it's a pain that
element.style.setProperty('x', 3.7);is rejected by TS when it's perfectly valid according to the spec.If we look at the DOMString definition:
We see that currently point 1 is handled (as seen with
valueabove). But Point 2 is ignored.If we look up the definition of
ToStringin the ecmascript spec we get:Steps 1-8 are easy to support by making DOMString map to:
Note that this type is
DOMStringInputnotDOMStringbecause the conversion only occurs on the JS -> DOMString side, and the type should not be used for IDL methods that returnDOMStringSteps 10-12 are tricky because it's not as simple as checking for a
toString(): stringmethod (as suggested above) because it can go unused iftoPrmitiveis defined (and if it is defined, we have no good way of knowingWhich is getting a bit convoluted. Anyway it's unclear if this change, which would allow more programs, would encourage correct programs, and should be supported or not, but some guidance from the project would be cool here.
HolgerJeromin commentedon Sep 5, 2025
In the past the maintainer stated that typescript should not mimic 100% the spec but be useful.
https://github.com/microsoft/TypeScript/wiki/TypeScript-Design-Goals