Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

DOMStringMap can lead to easy type errors #13792

Closed
Pinpickle opened this issue Jan 31, 2017 · 8 comments
Closed

DOMStringMap can lead to easy type errors #13792

Pinpickle opened this issue Jan 31, 2017 · 8 comments
Labels
Breaking Change Would introduce errors in existing code Domain: lib.d.ts The issue relates to the different libraries shipped with TypeScript Fixed A PR has been merged for this issue Help Wanted You can do this Suggestion An idea for TypeScript

Comments

@Pinpickle
Copy link

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.

@mhegazy mhegazy added Suggestion An idea for TypeScript Domain: lib.d.ts The issue relates to the different libraries shipped with TypeScript Help Wanted You can do this labels Jan 31, 2017
@mhegazy
Copy link
Contributor

mhegazy commented Jan 31, 2017

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

@Pinpickle
Copy link
Author

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

Is this a breaking change?

@mhegazy
Copy link
Contributor

mhegazy commented Jan 31, 2017

it is a breaking change. marking it as such.

@mhegazy mhegazy added the Breaking Change Would introduce errors in existing code label Jan 31, 2017
@Pinpickle
Copy link
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?

@mhegazy
Copy link
Contributor

mhegazy commented Jan 31, 2017

you need to override https://github.com/Microsoft/TSJS-lib-generator/blob/master/inputfiles/overridingTypes.json instead. not the .xml file.

@HolgerJeromin
Copy link
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?

@Pinpickle
Copy link
Author

@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?

@HolgerJeromin
Copy link
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.

@zhengbli zhengbli added the Fixed in TSJS repo Fix merged in https://github.com/Microsoft/TSJS-lib-generator, but not ported yet label Feb 2, 2017
@zhengbli zhengbli reopened this Feb 2, 2017
@mhegazy mhegazy added this to the TypeScript 2.2.1 milestone Feb 2, 2017
@mhegazy mhegazy added the Fixed A PR has been merged for this issue label Feb 2, 2017
@zhengbli zhengbli removed the Fixed in TSJS repo Fix merged in https://github.com/Microsoft/TSJS-lib-generator, but not ported yet label Feb 3, 2017
@microsoft microsoft locked and limited conversation to collaborators Jun 19, 2018
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
Breaking Change Would introduce errors in existing code Domain: lib.d.ts The issue relates to the different libraries shipped with TypeScript Fixed A PR has been merged for this issue Help Wanted You can do this Suggestion An idea for TypeScript
Projects
None yet
Development

No branches or pull requests

4 participants