TypeScript incorrectly considers variable as possibly null inside condition that checks variable
See original GitHub issueDescribe 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:
- Created 3 years ago
- Reactions:8
- Comments:21 (13 by maintainers)
Top 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 >Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start FreeTop Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Top GitHub Comments
Small update/summary:
#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.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