-
Notifications
You must be signed in to change notification settings - Fork 12.6k
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
User defined type guard function and type any #5930
Comments
Type guards do not narrow More specifically, we actually did try it the other way. The initial version of type guards did narrow on // By contract, this function accepts only a Dog or a House
function fn(x: any) {
// For whatever reason, user is checking against a base class rather
// than the most-specific class. Happens a lot with e.g. HTMLElement
if (x instanceof Animal) {
x.woof(); // Disallowed if x: Animal
} else {
// handle House case
}
} |
At the expense of being pedantic, this isn't entirely true. let x: any;
if (typeof x === "number") {
// 'x' has type 'number' here.
let y = x * x;
} |
@RyanCavanaugh what bothers me is that the design team is up for encouraging bad habits, this is wrong, please stop, you cannot follow the flow even if everyone flows with it, you should make what's right, not what's everyone is accustomed to be doing for their lives, i mean an average member of javascript community doesn't have a PhD in computer science, why the hell we choose to follow his bad habits? type guard should narrow things, this is what they do, narrowing |
+1 on changing the behavior to narrowing the type. With respect to prior user behavior, I don't think it's something to be encouraged, but if it is widespread, it's because Alternatively, maybe there could be some sort of syntax where the user can explicitly indicate intent to narrow? E.g. |
I'm serious about the number of breakages we found in real-world code. It was very large, and feedback from people trying early builds said they didn't want this. It's so far been basically impossible to get a type error on a value of type On the one hand, listen to users, and on the other hand, listen to users... |
Don't see a contradiction. Let |
What you've written there is runtime-invalid code. The point is that there's lots of runtime-valid code that also issues a type error if we narrow |
Let's not use word strictness or narrowing for a second. Type guard is a predicate that asserts at runtime that a given value is of certain type. Ultimately one can put anything in it and it would say yes or no. Once it said yes you get a guarantee (up to the implementation of the type guard) that a value is of that type. Am I right so far? If so, then your example with the |
I am not trying to convince you. Just ranting. I get it that the design goal is to make a mainstream language that fits the least competent developer. It is indeed very frustrating that correctness isn't the goal. I get that too. There is already quite a number of features that I wish didn't exist at all, we one more one less doesn't make a difference. Hopefully the niche for a correct/sound counterpart of TypeScript will be filled soon. As soon as we have it we will get a natural partitioning between the skilled developers and beginners. |
Hi all!
Why there is no compile error while acessing
nonExistsProp
? Is it by design and guard function only works with union types? Can't find some sort of specs, please, help me.The text was updated successfully, but these errors were encountered: