Skip to content

DOMStringMap can lead to easy type errors #13792

Closed

Description

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.

Activity

  1. added
    Suggestion
    Domain: lib.d.ts
    on Jan 31, 2017
  2. mhegazy commented on Jan 31, 2017

    @mhegazy
    Contributor

    This proposal will make the two lines flagged as errors now.. but i suppose it is safer.

  3. Pinpickle commented on Jan 31, 2017

    @Pinpickle
    Author

    That's a good point - I'd rather that happen. I will open a PR tomorrow.

    Is this a breaking change?

  4. mhegazy commented on Jan 31, 2017

    @mhegazy
    Contributor

    it is a breaking change. marking it as such.

  5. added
    Breaking Change
    on Jan 31, 2017
  6. Pinpickle commented on Jan 31, 2017

    @Pinpickle
    Author

    After doing some more research, it seems as though I need to modify https://github.com/Microsoft/TSJS-lib-generator/blob/master/inputfiles/browser.webidl.xml

    According to the readme of that repo, these are generated by Edge.

    Does this make the issue a non-starter?

  7. mhegazy commented on Jan 31, 2017

    @mhegazy
    Contributor
  8. HolgerJeromin commented on Feb 2, 2017

    @HolgerJeromin
    Contributor

    Every object map can be undefined on a random key access. Can we catch this in a general approach or do we have to find every needed definition?

  9. Pinpickle commented on Feb 2, 2017

    @Pinpickle
    Author

    Holger Jeromin (@HolgerJeromin) that's a good point. For numbers when access is array-like, including undefined doesn't make much sense but for strings it does. Does anyone else have opinions on this?

  10. HolgerJeromin commented on Feb 2, 2017

    @HolgerJeromin
    Contributor

    I think even with array-like access it is easy possible to get an undefined value.

    I just changed my own code and had an issue with for in loops.

    const StringDict: { [index: string]: string; } = {foo: 'bar'};
    for (let key in StringDict) {
       const thisStringValue = StringDict[key];
    }
    const StringDict2: { [index: string]: string | undefined; } = {foo: 'bar'};
    for (let key in StringDict2) {
       const StringValueOrUndefined = StringDict2[key];
    }
    

    I cannot imagine a case where StringValueOrUndefined is really able to become undefined. So an extra check to make TS happy would burn CPU cycles.

  11. added
    Fixed in TSJS repo
    on Feb 2, 2017
  12. added
    Fixed
    on Feb 2, 2017
  13. removed
    Fixed in TSJS repo
    on Feb 3, 2017
  14. locked and limited conversation to collaborators on Jun 19, 2018
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    Breaking ChangeDomain: lib.d.tsFixedSuggestion

    Type

    No type

    Projects

    No projects

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions