Skip to content

Automatic adding undefined to dictionary maps #14144

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

Closed
HolgerJeromin opened this issue Feb 17, 2017 · 2 comments
Closed

Automatic adding undefined to dictionary maps #14144

HolgerJeromin opened this issue Feb 17, 2017 · 2 comments
Labels
Working as Intended The behavior described is the intended behavior; this is not a bug

Comments

@HolgerJeromin
Copy link
Contributor

My suggestion was hidden in the closed #13792, so i open a new issue about that.

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?

I just changed my own code and had an issue with for in loops. Tested with TS2.1 perhaps this has changed in 2.2

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.

@RyanCavanaugh
Copy link
Member

I cannot imagine a case where StringValueOrUndefined is really able to become undefined.

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

So an extra check to make TS happy would burn CPU cycles.

This is why the postfix ! operator exists.

@RyanCavanaugh RyanCavanaugh added the Working as Intended The behavior described is the intended behavior; this is not a bug label Feb 17, 2017
@HolgerJeromin
Copy link
Contributor Author

One of the questions was if this | undefined should be added by typescript itself for such constructions.

@mhegazy mhegazy closed this as completed Apr 21, 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
Working as Intended The behavior described is the intended behavior; this is not a bug
Projects
None yet
Development

No branches or pull requests

3 participants