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

Object entry remains typed as undefined after assignment #33073

Closed
Ionaru opened this issue Aug 25, 2019 · 4 comments
Closed

Object entry remains typed as undefined after assignment #33073

Ionaru opened this issue Aug 25, 2019 · 4 comments
Labels
Duplicate An existing issue was already created

Comments

@Ionaru
Copy link

Ionaru commented Aug 25, 2019

TypeScript Version: 3.7.0-dev.20190824

Search Terms:
Object is possibly undefined
TS2532

Code

interface IData {
  data: number;
}

interface IMyObject {
  [index: string]: IData | undefined;
}

const myObject: IMyObject = {};
const objectKey = 'something';

myObject[objectKey] = {
  data: 5
}

console.log(myObject[objectKey].data); // TS2532: Object is possibly 'undefined'

Expected behavior:
Code compiles without error.

Actual behavior:
Error: TS2532: Object is possibly 'undefined'. on myObject[objectKey]

Playground Link:
http://www.typescriptlang.org/play/index.html#code/JYOwLgpgTgZghgYwgAgJIBE5jsg3gKGWQBMs4AuZEAVwFsAjaAbnwF999RJZEVUBZAJ4B5egCsICMHkLIA2qGIQAHpQDOYKKADmAXUoYyyAD7JqIJTFARiLdvgQB7EBuS0R4yWANDREqcgAvHisLE4u0o6eUgDSEIJByADkao60EGAAFjpJLPjufl5yUf5gcYK6iQREpNiUAKxsHOGpADYQAHStjtoAFAXRYMWD5bodtXAAlExAA

Related Issues:
#13778, #17960, #26599, #29642

It seems related issues have been around for a long time, in addition issue #26599 was closed as a "wontfix". This is weird to me because it seems Typescript should be able to detect that objectKey has not changed between the object assignment and query.

@AnyhowStep
Copy link
Contributor

If anyone else is looking for a workaround,

interface IData {
  data: number;
}

interface IMyObject {
  [index: string]: IData | undefined;
}

const myObject: IMyObject = {};
const objectKey = 'something';

myObject.something = {
  data: 5
}

console.log(myObject.something.data);

Playground

Just don't use string literals/constants. Use dot notation instead.

@Ionaru
Copy link
Author

Ionaru commented Aug 25, 2019

Just don't use string literals/constants. Use dot notation instead.

Good solution, but doesn't work when objectKey is a variable :)

What I posted is just a demonstration of the issue, in most cases the object keys are not constant but variables or parameters passed to a function.

@fatcerberus
Copy link

If the key is truly variable, then this is related to #31445: TS isn't equipped to understand that two mentions of obj[key] access the same property in both places; since it doesn't even know that, it doesn't make sense to perform any narrowing on it.

@sandersn sandersn added the Duplicate An existing issue was already created label Aug 29, 2019
@sandersn
Copy link
Member

Yep, duplicate of #31445 (and some others).

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Duplicate An existing issue was already created
Projects
None yet
Development

No branches or pull requests

4 participants