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.

TypeScript incorrectly considers variable as possibly null inside condition that checks variable

See original GitHub issue

Describe the bug TypeScript error “Object is possibly null” is shown for nested conditional checks.

To Reproduce Simple test case:

<script lang="typescript">
	interface TestObject {
		author?: string;
	}
	export let test: TestObject | null;
</script>

{#if test}
    <div>
        {#if test.author}
	        <div>Author: {test.author}</div>
	    {/if}
    </div>
{/if}

TypeScript throws error Object is possibly null on line {#if test.author}, not specifically on test inside that line.

Expected behavior Expected not to throw any errors.

System (please complete the following information):

  • OS: OSX 10.15.7
  • IDE: VSCode
  • Plugin/Package: “Svelte for VSCode”

Additional context If nested conditional statement is removed and in TestObject interface author? is replaced with author (to make it required), it would be logical for TypeScript to throw the same error for {test.author}, but it doesn’t. So looks like error is triggered by nested conditional statement, without it TypeScript knows that test is not null.

Issue Analytics

  • State:closed
  • Created 3 years ago
  • Reactions:8
  • Comments:21 (13 by maintainers)

github_iconTop GitHub Comments

4reactions
dummdidummcommented, Feb 21, 2021

Small update/summary:

  • control flow is reset as soon as the code is inside one of these constructs: #await, #each, let:X. This is due to the way we have to transform the code. We haven’t forgotten about this issue and are still looking for ways to solves this in a good way.
  • nested if-conditions now should work as expected as long as they are not interrupted by one of the things mentioned in the first bullet point
4reactions
jasonlyu123commented, Oct 19, 2020

Right now you can’t use typescript in markup. There’s no way to preprocess markup or let compiler parse typescript syntax. So non-null assertion is not possible. Haven’t tried the viability buy maybe we could copy the upper-level condition and append it before the nested condition

 {() => {if (test && test.author){<>
	        <div>Author: {test.author}</div>
	    </>}}}
Read more comments on GitHub >

github_iconTop Results From Across the Web

How to suppress "error TS2533: Object is possibly 'null' or ...
Personally I use null s in "vanilla" JavaScript to initialize variables or properties values. This gives me straight answer if given var or...
Read more >
How to fix "object is possibly null" in TypeScript? - Tim Mousk
One of the methods to fix the "object is possibly null" error in TypeScript is to use the optional chaining operator, like so:...
Read more >
Documentation - Everyday Types - TypeScript
In this chapter, we'll cover some of the most common types of values you'll find in JavaScript code, and explain the corresponding ways...
Read more >
Understanding the exclamation mark in TypeScript
What the exclamation mark does in TypeScript. Let's say a variable is defined as possibly null or undefined, like so:
Read more >
Nullish Coalescing: The ?? Operator in TypeScript
You might be wondering why the compiler emits the following expression to check the value variable against null and undefined : value !==...
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