You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
The below code is a super simplified version of a real world scenario I just ran into. When trying to assign a value only if some other condition is true and otherwise returning undefined, TypeScript produces a strange union type that includes empty string ''.
The linked Playground contains a slightly larger example that gets closer to demonstrating my real world use case.
I would argue that in cases where the compiler can clearly know that something is truthy, it should evaluate to the type of the value on the right hand side of &&, even with strictNullChecks off.
The below code is a super simplified version of a real world scenario I just ran into. When trying to assign a value only if some other condition is true and otherwise returning
undefined
, TypeScript produces a strange union type that includes empty string''
.The linked Playground contains a slightly larger example that gets closer to demonstrating my real world use case.
I would argue that in cases where the compiler can clearly know that something is truthy, it should evaluate to the type of the value on the right hand side of
&&
, even withstrictNullChecks
off.TypeScript Version: 3.5.1 (With
strictNullChecks
off)Search Terms:
logical and empty string
,string && empty
Code
Expected behavior:
Type of
x
is'a'
Actual behavior:
Type of
x
is'' | 'a'
Playground Link:
https://www.typescriptlang.org/play/?strictNullChecks=false#code/C4TwDgpgBAysBOBLAdgcwKrMQe2VAvFAOQCGRUAPsQEZECwAUIwGYCuyAxsDnqhMHFbNmACgCUALlgIUGLLkpR2AEwjMUEZVADejKPqgdcAZ2BQANthKqthBKwgBuPQfj9W8PAFkSwABYAdPAkyMrYALbiUAD8FlY2UABkicRkUFIqahrKzgwAvkA
Related Issues:
Couldn't find any 🕵🏼♂️
The text was updated successfully, but these errors were encountered: