microsoft / microsoft/TypeScript

Control flow not formed when using consts

Open
#11,542 4 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

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

Description

TypeScript Version: 2.0.3

Code


interface Releasable {
    release(): void;
}

const typeOfNumber: string = 'number';

function release(releasable: number | Releasable): void {
    if(typeof releasable === typeOfNumber) {
         allocated.splice(allocated.indexOf(releasable), 1);
    } else {
         releasable.release();
    }
}

Expected behavior:


interface Releasable {
    release(): void;
}

const typeOfNumber: string = 'number';

function release(releasable: number | Releasable): void {
    if(typeof releasable === typeOfNumber) {
         allocated.splice(allocated.indexOf(releasable), 1); // Ok, typeof releasable is number here
    } else {
         releasable.release(); // Ok, typeof releasable implements Releasable here
    }
}

Actual behavior:


interface Releasable {
    release(): void;
}

const typeOfNumber: string = 'number';

function release(releasable: number | Releasable): void {
    if(typeof releasable === typeOfNumber) {
         allocated.splice(allocated.indexOf(releasable), 1); // [ts] Argument of type 'number | Releasable' is not assignable to parameter of type 'number'. Type 'Releasable' is not assignable to type 'number'.
     } else {
         releasable.release(); // [ts] Property 'release' does not exist on type 'number | Releasable'.
     }
}

Currently type guards are only limited to literals. Type guards are consumed at compile time and from this taken in mind variables defined with let or var seems natural to not result in proper control flow but type guards using constants defined with const should result in a proper type guard. Since the compiler already knows that consts will not change their value ans so their value should be checked dose it evaluate to known typeof return value to form a proper control flow else to result in a [ts] error ...

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 by running the supplied TypeScript 2.0.3 example and compare the reported errors with the expected narrowing behavior. Trace the compiler's control-flow type-guard handling for typeof comparisons, then add coverage showing that an unchanged const string enables narrowing while mutable variables do not.

Written by the indexing model from the issue text.

Assessment

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

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.