Skip to content

filter noUncheckedIndexedAccess with own property check #41848

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

Open
5 tasks done
cookiesjuice opened this issue Dec 7, 2020 · 2 comments
Open
5 tasks done

filter noUncheckedIndexedAccess with own property check #41848

cookiesjuice opened this issue Dec 7, 2020 · 2 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

@cookiesjuice
Copy link

cookiesjuice commented Dec 7, 2020

Search Terms

ownProperty

hasOwnProperty

noUncheckedIndexedAccess

type check

Suggestion

noUncheckedIndexedAccess is a great feature. However, ts reports that object can be undefined even with hasOwnProperty check.

I suggest that a hasOwnProperty check makes unchecked indexed accesses checked, and return the checked value.

Use Cases

const obj: {[key: string]: string} = {};
// do something with obj
const key = "someKey";
if (Object.prototype.hasOwnProperty.call(obj, key)) {
  console.log(obj[key].length); // TS2532: Object is possibly 'undefined'.
}

Since only string values are allowed in obj, the only case such value is undefined is when the key does not exist on obj. The value of obj[key] can only be string. However, typescript still reports type of obj[key] being string | undefined.

Examples

const obj: {[key: string]: string} = {};
// do something with obj
const key = "someKey";
if (Object.prototype.hasOwnProperty.call(obj, key)) {
  console.log(obj[key].length); // no problem, since obj[key] has correct type string
}

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.
@MartinJohns
Copy link
Contributor

Related: #10530

@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 7, 2020
@magwas
Copy link

magwas commented Mar 31, 2023

Actually I consider the current behaviour to be a bug.

This playground reports two errors where I believe there should be none, as both cases are checked:

https://tsplay.dev/WG85Kw

Please tell me if I have to open a separate bug report either because it is a bug, or I am commenting to something unrelated.

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

4 participants