Skip to content

DOMString and friends are too narrow #1003

@relrelb

Description

@relrelb

DOMString, USVString, CSSOMString and ByteString are currently mapped to simply string:

...[...stringTypes].map((type) => [type, "string"] as [string, string]),

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.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions