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
function callOrNot1<T>(x: T | (() => T)): T {
return typeof x === "function" ? /* correctly errors here */ x() : x;
}
function callOrNot2<T>(valOrFunc: T | (() => T)): T {
return valOrFunc instanceof Function ? valOrFunc() : valOrFunc;
}
callOrNot2<() => string>(() => "")() // compiles, fails at runtime
Output
"use strict";
function callOrNot1(valOrFunc) {
return typeof valOrFunc === "function" ? valOrFunc() : valOrFunc;
}
function callOrNot2(valOrFunc) {
return valOrFunc instanceof Function ? 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