microsoft / microsoft/TypeScript

Infer const-ness of local let bindings that aren't assigned to

Open
#39,251 9 comments 2 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

Awaiting More Feedback Suggestion
Dominant language
Go
Stars
111k
Forks
14.3k
Avg merge
2d 4h
Merged PRs (30d)
132

Description

Search Terms

let const infer assignment

Suggestion

In some cases, type narrowing for const bindings is more effective than for let bindings. Specifically, using such bindings from a function that closes over them will keep the narrowed type, which doesn't happen for let binding, presumably on the assumption that those might be mutated at any time.

This suggests to treat local let bindings that are never assigned to (which can be determined by a rather straightforward static check on the syntax tree) like const bindings for this purpose.

(Interestingly, something like this seems to already exist for function parameters.)

Use Cases

Though some people have adopted a coding style where locals are declared with const unless assigned to, there's also a good argument to be made that that is not a very good use of brain cycles and just using let for all locals is fine. This proposal would improve type inference for people using the let style.

Examples

function foo() {
  let v: number[] | null = Math.random() < 0.5 ? [1] : null
  if (Array.isArray(v)) setTimeout(() => console.log(v.length), 100)
}

The compiler complains that v is possibly null. If the binding is changed to const the problem goes away. If v is made a function parameter it also doesn't occur. (But comes back when a v = null statement is added, suggesting a check like the one suggested here is being done in that case.)

Checklist

My suggestion meets these guidelines:

  • This wouldn't be a breaking change in existing TypeScript/JavaScript code
  • This wouldn't change the runtime behavior of existing JavaScript code
  • This could be implemented without emitting different JS based on the types of the expressions
  • This isn't a runtime feature (e.g. library functionality, non-ECMAScript syntax with JavaScript output, etc.)
  • This feature would agree with the rest of TypeScript's Design Goals.

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

Start with the supplied foo example and compare the proposed local-let behavior with the existing behavior for const bindings and function parameters. Define completion as preserving the narrowed type inside the closing callback when a local let binding has no assignments, while retaining the existing behavior when it can be mutated; add coverage for both cases.

Written by the indexing model from the issue text.

Assessment

Tech stack
typescript
Domain
compilers
Issue type
Feature
Difficulty
5/5
Estimated time
Over a week
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
35/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.