-
Notifications
You must be signed in to change notification settings - Fork 12.8k
hasOwnProperty return type #41915
New issue
Have a question about this project? # for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “#”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? # to your account
Comments
PS: if this can be 'fixed' for |
Duplicate of #18282. |
This is tracked by #21732. |
This would be amazing! Working with Take the following example: interface Arg {
field1: string;
field2: number;
}
function validateArg(val: unknown): val is Arg {
if (val == null || typeof val !== "object") {
return false;
}
val; // here `val` is `object`
if (typeof val.field1 !== "string") {
// ^-- error "field1" does not exist on "object"
}
if (!obj.hasOwnProperty("field1") || typeof val.field1 !== "string") {
// ^-- error "field1" does not exist on "object"
// ^^- hasOwnProperty doesn't change the deduced type of `val` at all
}
...
} Currently the only solution is to define this function itself and use it or cast This solution doesn't work for non-literals (as in it passes compilation but doesn't further narrow the type)./ |
@RyanCavanaugh #41915 (comment) |
Search Terms
hasOwnProperty
Suggestion
Use Cases
To be able to use the property of an object after it has been tested for its existence.
Current behavior:
Examples
https://www.typescriptlang.org/play?#code/JYOwLgpgTgZghgYwgAgPICMBWEFmQbwFgAoZM5AeguQGEB7AWwYnGToFc8wALYAZ2QAbUCjB1kfCCnTtggsAFpQydnzgBzCADoS5ZNzh9UAdxAAFKHQAO0MAE8APAGkId5BAAekEABMBF61s7FzsAPgAKADcALmQQgEpYnn5kFOSBADJkACUcOigfZ1cAGhUQAGsQOlNQgG4SAF8SEhh2EFxgOhBkSD4wcLgodVi2yuqQeIJdcmAYZHDm0j1yexs6OcH1ZABeXeQAcjosHDB96eXkDKzN5ABCPZB2QUFz5avkTa0DI1MAmyh7OF9r0wH99vFXnp3qsIOsPkMtCC-js9vs+lBQOp9pRqAA5fIMOCCdxQSxQWJ-IIHJGWKzYnx0CACKp4Tz8PBdHp2GwHI7YXD7LThABMAGZRQBOCHEV6TIhLC7GODgPgAZTAGJA6gGCJp1ni9QVyCaxBNLTaHU5IOFOuGZTGpjlr1m80WFzIMLhN12215xwFkPI7xu919j2egbI72BTNBtOxyk2kcuWU9G11seRPoO6Mx2KoyFVcGYJLJFNpVJjfTByAZTOQLPcHnZbG6ML9-NOQrFkulsqmRr0SpV6s12s+eqsBteJrNxFa7TAnW6w7Aao1mPC6Niua1cuQUAgYHYUG66NqxtqQA
Checklist
My suggestion meets these guidelines:
The text was updated successfully, but these errors were encountered: