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
Limited type should be assignable to original type.
The limitation is in effect within current scope.
π Motivating Example
When I was writing overloaded function, I thought I'm writing unnecessary code(like type guard) on runtime because I assumed the user always provide valid arguments.
So, why don't you use as keyword?
There are two ways to use as keyword.
First, you can redeclare variable with as keyword.
Second, you can put as keyword on EVERYWHERE the variable used.
I think the first way makes unnecessary variable.
And the second way is terrible as you know.
π» Use Cases
classA{}classBextendsA{foo(){}}constfoo: A=newB();if('foo'infoo){foo :=B;console.log(foo.foo());// no errors}
functionoverloaded(type: 'array',array: Array<any>): undefined;functionoverloaded(type: 'string',string: string): undefined;functionoverloaded(type: 'array'|'string',value: Array<any>|string){if(type==='array'){value :=Array<any>;console.log(value.every(element=>element!==undefined));// no errors}else{value :=string;console.log(value.substring(0,5));// no errors}}
The text was updated successfully, but these errors were encountered:
Suggestion
π Search Terms
type limit operator
β Viability Checklist
My suggestion meets these guidelines:
β Suggestion
This feature limits type for variable.
π Motivating Example
When I was writing overloaded function, I thought I'm writing unnecessary code(like type guard) on runtime because I assumed the user always provide valid arguments.
So, why don't you use
as
keyword?There are two ways to use
as
keyword.First, you can redeclare variable with
as
keyword.Second, you can put
as
keyword on EVERYWHERE the variable used.I think the first way makes unnecessary variable.
And the second way is terrible as you know.
π» Use Cases
The text was updated successfully, but these errors were encountered: