Skip to content

DOMStringMap can lead to easy type errors #13792

Closed
@Pinpickle

Description

@Pinpickle

TypeScript Version: 2.1.5 (bug is present on master)

Code

const element = document.createElement('div');

const dataset = element.dataset;
dataset['key1'] = 'Hello';

console.log(dataset['key1'].toUpperCase()); // HELLO
console.log(dataset['key2'].toUpperCase()); // Cannot read property 'toUpperCase' of undefined

Expected behavior:

The project doesn't compile with a type error.

Actual behavior:

The project does compile, and a runtime error occurs.

This is caused by the fact that DOMStringMap's index return has type string, and not string | undefined.

We want

interface DOMStringMap {
    [name: string]: string;
}

To be

interface DOMStringMap {
    [name: string]: string | undefined;
}

If this is approved, I am very happy to make a PR on this - and would like to do so to try and contribute to TypeScript codebase.

Metadata

Metadata

Assignees

No one assigned

    Labels

    Breaking ChangeWould introduce errors in existing codeDomain: lib.d.tsThe issue relates to the different libraries shipped with TypeScriptFixedA PR has been merged for this issueHelp WantedYou can do thisSuggestionAn idea for TypeScript

    Type

    No type

    Projects

    No projects

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions