microsoft / microsoft/TypeScript

strictNullChecks not recognizing evaluated key in object

Open
#15,705 1 comment 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

Bug Domain: check: Control Flow
Dominant language
Go
Stars
111k
Forks
14.3k
Avg merge
2d 4h
Merged PRs (30d)
132

Description

TypeScript Version: 2.3.2

Code
This version errors with strictNullChecks

export interface Opts {
    params?: { [s: string]: string };
}

let token;
let options: Opts = { params: {} };

if (token) {
    options.params["access_token"] = token;
}

This version does not error with strictNullChecks

export interface Opts {
    params?: { [s: string]: string };
}

let token;
let options: Opts = {};

options.params = {};

if (token) {
    options.params["access_token"] = token;
}

Expected behavior:
TS should recognize that params has been set in the object like it already does using a differently formatted method of initializing the variable.

Actual behavior:
TS fails to recognize that params has been set.

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Reproduce both TypeScript 2.3.2 snippets with strictNullChecks enabled and compare their control-flow analysis of the optional params property. Trace the strictNullChecks type-checking entry point that handles property assignments and indexed access. Done means the object-literal initialization form recognizes params as assigned consistently with the separately assigned form.

Written by the indexing model from the issue text.

Assessment

Tech stack
typescript
Domain
compilers
Issue type
Bug
Difficulty
4/5
Estimated time
3-5 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
35/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.