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
{{ message }}
Repository navigation
Type guard does not work correctly with empty string type #42101
The compiled JavaScript version works correctly, but TypeScript gives errors:
typeEmptyString=''|null|undefined;functionisEmpty(value: string|EmptyString): value is EmptyString{returnvalue===''||value===null||value===undefined;}lettest: string|null|undefined;if(isEmpty(test)){if(test===''){// Compilation error since test: null | undefined and not test: '' | null | undefinedconsole.log('Nope');}}
Also, it's weird that when I remove null from the list of types, the error disappears, but the incorrect type is still shown:
typeEmptyString=''|undefined;functionisEmpty(value: string|EmptyString): value is EmptyString{returnvalue===''||value===undefined;}lettest: string|undefined;if(isEmpty(test)){if(test===''){// No errors, but the editor displays incorrect type.console.log('Nope');}}
🙁 Actual behavior
TypeGuard ignores empty string and thinks the value can only be null or undefined.
🙂 Expected behavior
The type should include empty string. No errors expected.
Activity
MartinJohns commentedon Dec 24, 2020
This looks very similar to #31156.
typescript-bot commentedon Dec 31, 2020
This issue has been marked as a 'Duplicate' and has seen no recent activity. It has been automatically closed for house-keeping purposes.