You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
interfaceReleasable{release(): void;}consttypeOfNumber: string='number';functionrelease(releasable: number|Releasable): void{if(typeofreleasable===typeOfNumber){allocated.splice(allocated.indexOf(releasable),1);// Ok, typeof releasable is number here}else{releasable.release();// Ok, typeof releasable implements Releasable here}}
Actual behavior:
interfaceReleasable{release(): void;}consttypeOfNumber: string='number';functionrelease(releasable: number|Releasable): void{if(typeofreleasable===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 ...
The text was updated successfully, but these errors were encountered:
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
TypeScript Version: 2.0.3
Code
Expected behavior:
Actual behavior:
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
orvar
seems natural to not result in proper control flow but type guards using constants defined withconst
should result in a proper type guard. Since the compiler already knows thatconst
s will not change their value ans so their value should be checked dose it evaluate to knowntypeof
return value to form a proper control flow else to result in a[ts] error
...The text was updated successfully, but these errors were encountered: