Skip to content

strictNullChecks - property accesses on string index signatures should narrow #17960

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
VianneyCarton opened this issue Aug 22, 2017 · 4 comments
Labels
Bug A bug in TypeScript
Milestone

Comments

@VianneyCarton
Copy link

TypeScript Version: 2.6.0-dev.20170819

Code

// process.env is defined as { [key: string]: string | undefined } in @types/node

// Expected
console.log(process.env.MY_ENV_VAR.length);  // TS2532: Object is possibly 'undefined'

if (process.env.MY_ENV_VAR) {
  // Unexpected - process.env.MY_ENV_VAR is defined here
  console.log(process.env.MY_ENV_VAR.length); // TS2532: Object is possibly 'undefined'
}

/* ------------------------------------------------------------------------------------- */

const collection: { [id: string]: string } = {};

console.log(collection.someVar.length); // No error

Expected behavior:
When using strictNullChecks :

  • The compiler should not emmit an TS2532: Object is possibly 'undefined' error when accessing an index of an object defined as { [key: string]: string | undefined } inside a guard.
  • The compiler should emmit an error when accessing an index of an object defined as { [key: string]: string } inside a guard without checking it first.

Actual behavior:
Please see code sample.

@mhegazy
Copy link
Contributor

mhegazy commented Aug 22, 2017

The second issue is tracked by #13778.

@mhegazy mhegazy added the Bug A bug in TypeScript label Aug 22, 2017
@ithinkihaveacat
Copy link

It's also surprising that assigning the (theoretically) type narrowed object to a different variable removes the error:

if (process.env.MY_ENV_VAR) {
  console.log(process.env.MY_ENV_VAR.length); // ERROR
}

const v = process.env.MY_ENV_VAR;
if (v) {
  console.log(v.length); // NO ERROR
}

v is seemingly equivalent to process.env.MY_ENV_VAR; why do they behave differently?

@mhegazy mhegazy added this to the TypeScript 2.9 milestone Apr 12, 2018
@sandersn sandersn changed the title strictNullChecks - unexpected behavior with string index types strictNullChecks - property accesses on string index signatures should narrow Jun 28, 2018
@mhegazy mhegazy modified the milestones: TypeScript 3.0, Future Jul 2, 2018
@thorn0
Copy link

thorn0 commented May 7, 2019

Another seemingly related error (TS 3.4.5):

declare const foo: {
    [id: string]: {
        bar?: {
            baz?: boolean;
        }
    }
};
const a1 = foo.id && foo.id.bar && foo.id.bar.baz; // works fine
const key = 'id';
const a2 = foo[key] && foo[key].bar && foo[key].bar.baz; // error: Object is possibly 'undefined'

playground

@jakebailey
Copy link
Member

Just looking at old issues; this one was fixed by #19838.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Bug A bug in TypeScript
Projects
None yet
Development

No branches or pull requests

6 participants