https://github.com/Microsoft/TypeScript/wiki/Breaking-Changes#narrowing-functions-now-intersects--object-and-unconstrained-generic-type-parameters
TypeScript Version: 3.8.3
Search Terms:
narrowing instanceof
narrowing functions
Expected behavior:
instanceof should match typeof (intersect over unconstrained T)
Actual behavior:
divergence
Related Issues:
Code
functioncallOrNot1<T>(x: T|(()=>T)): T{returntypeofx==="function" ? /* correctly errors here */x() : x;}functioncallOrNot2<T>(valOrFunc: T|(()=>T)): T{returnvalOrFuncinstanceofFunction ? valOrFunc() : valOrFunc;}callOrNot2<()=>string>(()=>"")()// compiles, fails at runtimeOutput
"use strict";functioncallOrNot1(valOrFunc){returntypeofvalOrFunc==="function" ? valOrFunc() : valOrFunc;}functioncallOrNot2(valOrFunc){returnvalOrFuncinstanceofFunction ? valOrFunc() : valOrFunc;}callOrNot2(()=>"")();Compiler Options
{
"compilerOptions": {
"noImplicitAny": true,
"strictNullChecks": true,
"strictFunctionTypes": true,
"strictPropertyInitialization": true,
"strictBindCallApply": true,
"noImplicitThis": true,
"noImplicitReturns": true,
"alwaysStrict": true,
"esModuleInterop": true,
"declaration": true,
"experimentalDecorators": true,
"emitDecoratorMetadata": true,
"moduleResolution": 2,
"target": "ES2017",
"jsx": "React",
"module": "ESNext"
}
}Playground Link:Provided
https://github.com/Microsoft/TypeScript/wiki/Breaking-Changes#narrowing-functions-now-intersects--object-and-unconstrained-generic-type-parameters
TypeScript Version: 3.8.3
Search Terms:
narrowing instanceof
narrowing functions
Expected behavior:
instanceofshould matchtypeof(intersect over unconstrained T)Actual behavior:
divergence
Related Issues:
Code
Output
Compiler Options
{ "compilerOptions": { "noImplicitAny": true, "strictNullChecks": true, "strictFunctionTypes": true, "strictPropertyInitialization": true, "strictBindCallApply": true, "noImplicitThis": true, "noImplicitReturns": true, "alwaysStrict": true, "esModuleInterop": true, "declaration": true, "experimentalDecorators": true, "emitDecoratorMetadata": true, "moduleResolution": 2, "target": "ES2017", "jsx": "React", "module": "ESNext" } }Playground Link:Provided