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.

assert incorrectly narrows type

See original GitHub issue

This code:

import { assert } from "tsafe";
const x: string | undefined = Math.random() < 0.5 ? undefined : "wtf";
assert(x);
let y: string = x;
console.log(y);

should not compile, but it does. Half the time it will console.log undefined and half the time will console.log wtf. Yet the type of y is supposedly a string.

It’s not obvious why, so to avoid the spoiling the fun the explanation is base64 encoded: QXNzZXJ0IGlzIGRlZmluZWQgd2l0aCBgY29uZGl0aW9uOiBhbnkgPSB0cnVlYCB3aGljaCBjb21waWxlcyB0byAiaWYgY29uZGl0aW9uIGlzIHVuZGVmaW5lZCwgbWFrZSBpdCB0cnVlIiB3aGlsZSB0aGUgdHlwZSBzaWduYXR1cmUgZG9lc24ndCByZWZsZWN0IHRoaXMuIA==

Issue Analytics

  • State:closed
  • Created 2 years ago
  • Reactions:1
  • Comments:8 (8 by maintainers)

github_iconTop GitHub Comments

1reaction
husseinherecommented, Feb 15, 2022

@garronej The example is actually quite relevant.

What do you think the outcome of my example is?

Presumedly you think it will do this:

When you write assert(x) you say to typescript “Hey listen, trust be I know x will be truthy, if ever it isn’t the case at runtime an exception will be thrown here so, please, assume for the rest of the execution that x is truthy”.

But it doesn’t. Run it and see ;P It does not throw a runtime exception, even though it narrows the type as if it did.

0reactions
garronejcommented, Feb 15, 2022

Hi @husseinhere,
I fixed the code after merging it. The condition deeded to be optional and that was it.

Read more comments on GitHub >

github_iconTop Results From Across the Web

narrowing types via type guards and assertion functions - 2ality
The problem in this case is that, without narrowing, we can't access property .second of a value whose type is FirstOrSecond .
Read more >
FR: assertions that throw could narrow types · Issue #45280
After a type narrowing assertion which throws, the type can be narrowed. This is a common pattern in Closure codebases. function assert<T> ...
Read more >
TypeScript: type narrowing: assert not undefined and hence ...
There's a longstanding bug/issue whereby TypeScript does not perform control flow analysis to narrow the type of a property if the property ...
Read more >
Documentation - Narrowing - TypeScript
The in operator narrowing​​ JavaScript has an operator for determining if an object has a property with a name: the in operator. TypeScript...
Read more >
Type Narrowing in TypeScript - Medium
TypeScript exploits this information and narrows the type of x from unknown to number in the body of if statement. This article will...
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