question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Potential Optional Chaining Bug

See original GitHub issue

TypeScript Version: 3.7.x-dev.201xxxxx

Search Terms:

Optional Chaining Type bug

Code

We found a bug within our codebase where a specific instance is returning “undefined” within production code. Upon review, we found this instance and recreated it here. Basically, TypeScript knows that “value” is associated to “string[] | undefined”, however, it ignores “undefined” when “.includes” is added onto it for most instances and acts like it is “string[]”.

interface Model {
    MyField: FieldModel
}

interface FieldModel {
    value: string[]
}

type ValidateFn<Model, Key extends keyof Model> = (
    value: Partial<Model[Key]>,
    attributes: Partial<Model>
) => void


const validator:ValidateFn<Model, 'MyField'> = (v, attributes) => {
    // value is 'string[] | undefined', yet, shows no error for value/includes.
    attributes.MyField?.value.includes('test')

    const MyField = attributes.MyField
    // value is 'string[] | undefined', yet, shows no error for value/includes.
    MyField?.value.includes('test')

    const value = attributes.MyField?.value
    // value is 'string[] | undefined'. This is the only one that shows an error for value/includes.
    value.includes('test')
}

Expected behavior:

Expected an error associated to “value” in each instance as value is a “string[] | undefined”.

Actual behavior:

An error is only returned on the last line. The previous lines are not showing an error. Thus, this would create a false positive that “undefined” is handled to the developer (unless they test or check it manually).

Playground Link:

Playground

Related Issues:

Issue Analytics

  • State:open
  • Created 3 years ago
  • Reactions:1
  • Comments:12 (11 by maintainers)

github_iconTop GitHub Comments

1reaction
RyanCavanaughcommented, Nov 19, 2021

@marafat that’s unrelated; please log a new issue. Thanks!

1reaction
CyrusNajmabadicommented, Nov 20, 2020

I’d be surprised to see a different type between x and b in the following example.

I personally disagree. These are two separate things. If i have a?.b then if i hover on b i want to know what b actually is as that will dictate what i can do with it as i continue that expression. For example a?.b.x The things i can dot off of b will be based on b’s actual type, not the type of the entire a?.b expression.

It’s the difference between a?.b. vs (a?.b).. Since there is no parenthesization, and since we’re examining the RHS, i think it only makes sense to to state what b actually is. If we didn’t, it would actually be more confusing as it would imply that you could get a different set of results than what you actually get.

Read more comments on GitHub >

github_iconTop Results From Across the Web

What is wrong with optional chaining and how to fix it - DEV ...
Edit: There is nothing wrong with optional chaining, the feature is related to idiomatic absence value in JS, and it is "null |...
Read more >
Implement the Optional Chaining operator (?.) proposal
Bug 1566143 - Update parser for optional chaining ... we would have potential errors with automatic semicolon insertion, thus making it impossible for...
Read more >
Optional Chaining - Bug Reports - RunKit Discuss
Hi @croolsby, I am well aware that it is built in to Node 14, that is how it is possible for it to...
Read more >
Optional Chaining - Swift.org
Optional chaining is a process for querying and calling properties, ... when the optional is nil , whereas forced unwrapping triggers a runtime...
Read more >
Optional chaining operator on for loop length - Stack Overflow
ts(2532) (my code works fine, just want to get rid of this error.) i can fix it like this but its not right...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found