-
Notifications
You must be signed in to change notification settings - Fork 12.5k
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
allow storing results of narrowing in booleans for further narrowing #24865
Comments
looks like you want the compiler to automatically infer dependent types for you :) |
i merely ask to extend booleans (in the local scope) with knowledge of what's been narrowed |
If I understand it correctly this is similar to #24983 ? I don't see a like this? : function twostep(x: symbol | string) {
const isSymbol = typeof x === "symbol";
if (!isSymbol) {
console.log(`Type of x is still string | symbol here: ${x}`);
}
}
function onestep(x: symbol | string) {
if (typeof x !== "symbol") {
console.log(`Type of x is now just string: ${x}`);
}
} |
Instead of inferring declare function isNumber(v: any): v is number;
function infer(x: string | number) {
// currently inferred as 'boolean'
// now inferred as 'x is number';
const intermediate = isNumber(x);
if (intermediate) {
x.toFixed();
} else {
x.substr();
}
}
// maybe also allow this syntax in type position
function syntactic(x: string | number) {
const intermediate: x is number = doSomething();
if (intermediate) {
x.toFixed();
} else {
x.substr();
}
} Caveat: the inferred type predicate needs to be invalidated at the next assignment of the narrowed variable ( |
I'd love to see this worked on. The motivation for me is this:
Code readability is very important; at present the behaviour of type narrowing enforces a code style which doesn't allow for meaningful variable names to drive control flow whilst maintaining narrowed types. I super miss that. |
Completely agree, I've found myself wanting to do this a lot as I like keeping the main body of logic as simple and human readable as possible.
The way I can see of resolving it is (as @ajafff said) to make type predicates (x is y) into proper types, so in add6 "nIsNumber" would be of type "n is number" rather than type "boolean". Note that this example is simplified to illustrate the problem but isn't a case where it would be particularly necessary. |
It'd be great if TS solved this problem. I strongly agree with @johnnyreilly that this limitation causes less-readable code. Note that the problem is broader than TypeScript-specific constructs like typeof and user-defined type guards. It also complicates migrating existing JavaScript code to TS when using common JS idioms like (example 2 below) preventing property access on a may-be-null object. It even breaks switch statements (example 3 below) where there's no intermediate local variables at all! Also, the problem doesn't only apply to boolean values. It applies to values of any type that may be used in a truthy/falsy context, like examples 2 and 3 below.
|
BTW, I think this is a dupe of #12184. |
I find myself coming back here every few weeks and reading this issue and every related issue in hopes this will be implemented. I like typescript, but this is the most frustrating thing I've encountered using it. |
I would love to see this working, sadly it don't seem to come soon. As a work around I cast the type when the error pop's up, but this isn't a good approach and I don't recommend it very much since if the BTW I think is good to ask, how to make a good proposal? |
Hell yeah |
Search Terms
https://github.com/Microsoft/TypeScript/search?q=type+guards+constants+narrowing&type=Issues
Suggestion
There is no way to use a saved result of a type guard evaluation for narrowing. Allow narrowing by
Use Cases
Examples
Checklist
My suggestion meets these guidelines:
The text was updated successfully, but these errors were encountered: