Quantcast
Viewing latest article 36
Browse Latest Browse All 106942

RSRP-472837: Type can be narrowed down through checking if a property exists

Reporter Image may be NSFW.
Clik here to view.
Maria Pleskunina (Maria.Pleskunina)
Maria Pleskunina (Maria.Pleskunina)
Created Dec 27, 2018, 3:58:20 PM
Updated Oct 10, 2019, 12:49:46 PM
Subsystem TypeScript
Assignee Nikita Popov (poksh)
Priority Normal
State Open
Type Bug
Fix version Backlog
Affected versions 2018.3, 2018.2.3, 2019.3
Fixed In EAP Undefined
VsVersion All Versions
Branch No branch

The following example produces an error message with Resharper but compiles without problems with tsc:

type A = { 
type: "info"; 
message: string; 
} | { 
type: "error"; 
error: string; 
};

declare let v: A;

if ("error" in v) // If error is in v we know that v must be an "error" object 
{ 
console.info(v.error); // so we should be able to access error here 
}

Viewing latest article 36
Browse Latest Browse All 106942

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>