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

Control flow not formed when using consts #11542

Open
NoHomey opened this issue Oct 11, 2016 · 4 comments
Open

Control flow not formed when using consts #11542

NoHomey opened this issue Oct 11, 2016 · 4 comments
Labels
Committed The team has roadmapped this issue Suggestion An idea for TypeScript
Milestone

Comments

@NoHomey
Copy link

NoHomey commented Oct 11, 2016

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 ...

@RyanCavanaugh
Copy link
Member

Why are you using a const rather than the literal "number" here?

We could think about this as a suggestion but you'd definitely need to not have the : string annotation of typeOfNumber since that explicitly discards our knowledge of its initializer

@mhegazy mhegazy added Suggestion An idea for TypeScript In Discussion Not yet reached consensus labels Oct 11, 2016
@NoHomey
Copy link
Author

NoHomey commented Oct 12, 2016

I'm using const as a refactor of typeof type guards and as well for little memory optimization. And I'm using string as type for the consts since typeof returns string: The typeof operator returns a string indicating the type of the unevaluated operand.

@NoHomey
Copy link
Author

NoHomey commented Oct 15, 2016

After reading the TypeScript Language Specification; 4.18.6 The typeof operator I've changed the type of typeOfNumber from string to primitive string literal 'number' but this dose not change the behavior (the same [ts] errors are emitted).

@RyanCavanaugh RyanCavanaugh added Committed The team has roadmapped this issue and removed In Discussion Not yet reached consensus labels Nov 15, 2016
@RyanCavanaugh RyanCavanaugh added this to the TypeScript 2.2 milestone Nov 15, 2016
@RyanCavanaugh
Copy link
Member

Approved change for next release

@mhegazy mhegazy modified the milestones: TypeScript 2.2, TypeScript 2.3 Feb 2, 2017
@mhegazy mhegazy modified the milestones: TypeScript 2.3, Future Mar 24, 2017
@sandersn sandersn removed their assignment Jan 7, 2020
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Committed The team has roadmapped this issue Suggestion An idea for TypeScript
Projects
None yet
Development

No branches or pull requests

4 participants