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
declare <typecast> syntax that replaces the type of the identifier used in the typecast. Ideally this would work inside an if statement to only replace the part of the union narrowed to by the if statement. TS2352 should be raised if it would be raised by a normal (non-declared) typecast.
π» Use Cases
When you know that a variable is a certain type, but Typescript doesn't (i.e. untyped libraries), you need to create a new variable and use typecasts. This creates an additional variable.
Test Cases
const x = 2;
declare x as string;
x // string
const x = RiggedMath.random(); // provides 0-0.5 or MyTwo, but is typed as () => number
if (x > 0.5) {
declare x as RiggedMath.MyTwo;
}
x // number | MyTwo
The text was updated successfully, but these errors were encountered:
Duplicate of #10421. It's the exact same suggestion, just different keywords.
you need to create a new variable and use typecasts. This creates an additional variable.
You can use assertion-functions instead. This creates no new variable. Only disadvantage is that you require a function, and it has a close-to-zero runtime impact.
Suggestion
π Search Terms
is:issue declare as
β Viability Checklist
My suggestion meets these guidelines:
β Suggestion
declare <typecast>
syntax that replaces the type of the identifier used in the typecast. Ideally this would work inside an if statement to only replace the part of the union narrowed to by the if statement. TS2352 should be raised if it would be raised by a normal (non-declared) typecast.π» Use Cases
When you know that a variable is a certain type, but Typescript doesn't (i.e. untyped libraries), you need to create a new variable and use typecasts. This creates an additional variable.
Test Cases
The text was updated successfully, but these errors were encountered: