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
This would be used similarly to the as operator but would persist beyond a single usage. That way a developer can specify that a variable is a different type past a certain point, without declaring a new identity variable.
functionf(o: Type){// stuff// past this point `o` is guaranteed to be the below type// currently, one would need to declare a new variableleto2=oasType&{[K: string]: Type};// really hope nobody accidentally uses `o` instead of `o2` below this point,// especially since `o` and `o2` can be mutated!}
Examples
Kind of a dumb example, but here is one:
interfaceNode<T>{Next: Node<T>;Value: T;}functionfoo<T>(node?: Node<T>){constvalues=newArray<T>();lettarget=node&&node.Next;while(target){typeofnode=Node<T>;// node is now Node<T> !!!const{Value: value}=node;}}
Assigning to incompatible types
as operators error if you try to tell the compiler it is an incompatible type. The way to get around this is using as unknown as .... This could have something analogous. I would recommend a NonNullAssertion operator at the end of the type declaration being equivalent to overriding not being able to assign to an incompatible type.
letx="4"typeofx=4!;
This would also provide a workaround for all problems with the type system improperly understanding variable types. #30557 #31892
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.)
Search Terms
typeof equals statements
typeof = statements
persistent as statement
Suggestion
I would like to see a new kind of statement added to TypeScript, called the typeofEquals statement. It would look like this:
Use Cases
This would be used similarly to the
as
operator but would persist beyond a single usage. That way a developer can specify that a variable is a different type past a certain point, without declaring a new identity variable.Examples
Kind of a dumb example, but here is one:
Assigning to incompatible types
as
operators error if you try to tell the compiler it is an incompatible type. The way to get around this is usingas unknown as ...
. This could have something analogous. I would recommend a NonNullAssertion operator at the end of the type declaration being equivalent to overriding not being able to assign to an incompatible type.This would also provide a workaround for all problems with the type system improperly understanding variable types.
#30557
#31892
Checklist
My suggestion meets these guidelines:
The text was updated successfully, but these errors were encountered: