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

type | undefined not assignable to type regardless of null check from Record with noUncheckedIndexedAccess #51582

Closed
DerHerrGammler opened this issue Nov 18, 2022 · 4 comments
Labels
Duplicate An existing issue was already created

Comments

@DerHerrGammler
Copy link

DerHerrGammler commented Nov 18, 2022

Bug Report

🔎 Search Terms

  • null check
  • record
  • noUncheckedIndexedAccess

🕗 Version & Regression Information

  • This is the behavior in every version I tried, and I reviewed the FAQ for entries about noUncheckedIndexedAccess option and null check

⏯ Playground Link

Playground link with relevant code

💻 Code

const arr: string[] = []

function addDefinitions(defs: Record<string, string>): void {
    Object.keys(defs).forEach((type: string): void => {;
        if (defs[type] != undefined) {
            arr.push(defs[type]); // ❌ ts error
        }
    });
}

🙁 Actual behavior

In my code i get a Argument of type 'Definition | undefined' is not assignable to parameter of type 'Definition'. error. But as you see i check the object value with if (defs[type] != undefined). But at this.addDefinition(type, defs[type]); the error is thrown anyway.

The active option noUncheckedIndexedAccess will produces this error. But as you see there is a null check so that this error should not be thrown.

🙂 Expected behavior

Because of the null check this error should not be shown.

@RyanCavanaugh RyanCavanaugh added the Duplicate An existing issue was already created label Nov 18, 2022
@RyanCavanaugh
Copy link
Member

For performance/correctness reasons we don't narrow when the key is a variable (we can't tell if it's been mutated or not). This limitation was called out in the PR text: #39560. noUncheckedIndexedAccess is extremely conservative in this regard.

@fatcerberus
Copy link

Workaround: Assign defs[type] to a temp variable and narrow that.

@DerHerrGammler
Copy link
Author

DerHerrGammler commented Nov 18, 2022

So in those cases best pratice is something like first assign to a variable and checking the variable against null
Like this:

const arr: string[] = []

function addDefinitions(defs: Record<string, string>): void {
    Object.keys(defs).forEach((type: string): void => {;
        const def: string | undefined = defs[type] // <<< assign to const
        if (def != undefined) {
            arr.push(def)
        }
    });
}

Or is there any better solution for this?

@DerHerrGammler
Copy link
Author

I just found out, that i can use Object.entries like the following and there is no error thrown:

const arr: string[] = []

function addDefinitions(defs: Record<string, string>): void {
    Object.entries(defs).forEach(([, def]): void => {;
        arr.push(def)
    });
}

thanks you all for the quick help

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Duplicate An existing issue was already created
Projects
None yet
Development

No branches or pull requests

3 participants