Skip to content
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

hasOwnProperty return type #41915

Open
5 tasks done
WORMSS opened this issue Dec 10, 2020 · 6 comments
Open
5 tasks done

hasOwnProperty return type #41915

WORMSS opened this issue Dec 10, 2020 · 6 comments
Labels
Awaiting More Feedback This means we'd like to hear from more people who would be helped by this feature Suggestion An idea for TypeScript

Comments

@WORMSS
Copy link

WORMSS commented Dec 10, 2020

Search Terms

hasOwnProperty

Suggestion

interface Object {
    hasOwnProperty<Key extends PropertyKey>(v: Key): this is this & Record<Key, unknown>;
}

Use Cases

To be able to use the property of an object after it has been tested for its existence.
Current behavior:

// Property 'testProp' does not exist on type 'object'.(2339)
obj.hasOwnProperty('testProp') && typeof arg.testProp === 'string' 

Examples

https://www.typescriptlang.org/play?#code/JYOwLgpgTgZghgYwgAgPICMBWEFmQbwFgAoZM5AeguQGEB7AWwYnGToFc8wALYAZ2QAbUCjB1kfCCnTtggsAFpQydnzgBzCADoS5ZNzh9UAdxAAFKHQAO0MAE8APAGkId5BAAekEABMBF61s7FzsAPgAKADcALmQQgEpYnn5kFOSBADJkACUcOigfZ1cAGhUQAGsQOlNQgG4SAF8SEhh2EFxgOhBkSD4wcLgodVi2yuqQeIJdcmAYZHDm0j1yexs6OcH1ZABeXeQAcjosHDB96eXkDKzN5ABCPZB2QUFz5avkTa0DI1MAmyh7OF9r0wH99vFXnp3qsIOsPkMtCC-js9vs+lBQOp9pRqAA5fIMOCCdxQSxQWJ-IIHJGWKzYnx0CACKp4Tz8PBdHp2GwHI7YXD7LThABMAGZRQBOCHEV6TIhLC7GODgPgAZTAGJA6gGCJp1ni9QVyCaxBNLTaHU5IOFOuGZTGpjlr1m80WFzIMLhN12215xwFkPI7xu919j2egbI72BTNBtOxyk2kcuWU9G11seRPoO6Mx2KoyFVcGYJLJFNpVJjfTByAZTOQLPcHnZbG6ML9-NOQrFkulsqmRr0SpV6s12s+eqsBteJrNxFa7TAnW6w7Aao1mPC6Niua1cuQUAgYHYUG66NqxtqQA

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.)
  • This feature would agree with the rest of TypeScript's Design Goals.
@WORMSS
Copy link
Author

WORMSS commented Dec 10, 2020

PS: if this can be 'fixed' for 'testProp' in obj that would be amazing too.

@MartinJohns
Copy link
Contributor

Duplicate of #18282.

@MartinJohns
Copy link
Contributor

if this can be 'fixed' for 'testProp' in obj that would be amazing too.

This is tracked by #21732.

@WORMSS
Copy link
Author

WORMSS commented Dec 10, 2020

Duplicate of #18282.

You say it's a duplicate, but it seems their example was a far inferior attempt?
None of the examples in #18282 would be as seamless as this is this & Record<Key, unknown>
Maybe due to 3 years ago when TS was not as mature.

@RyanCavanaugh RyanCavanaugh added Awaiting More Feedback This means we'd like to hear from more people who would be helped by this feature Suggestion An idea for TypeScript labels Dec 10, 2020
@Nokel81
Copy link

Nokel81 commented Jan 12, 2021

This would be amazing! Working with unknown is really annoying currently because even after doing an explicit check (what will happen at runtime) it still treats it as non existing.

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 obj to Record<string, unknown> and go from there.

This solution doesn't work for non-literals (as in it passes compilation but doesn't further narrow the type)./

@Malix-Labs
Copy link

@RyanCavanaugh #41915 (comment)
What feedback still needs to be awaited?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Awaiting More Feedback This means we'd like to hear from more people who would be helped by this feature Suggestion An idea for TypeScript
Projects
None yet
Development

No branches or pull requests

5 participants