Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

narrow from 'any' in most situations #10319

Closed
wants to merge 5 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 12 additions & 5 deletions src/compiler/checker.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8548,10 +8548,6 @@ namespace ts {
}
return type;
}
// We never narrow type any in an instanceof guard
if (isTypeAny(type)) {
return type;
}

// Check that right operand is a function type with a prototype property
const rightType = checkExpression(expr.right);
Expand All @@ -8569,6 +8565,11 @@ namespace ts {
}
}

// Don't narrow from 'any' if the target type is exactly 'Object' or 'Function'
if (isTypeAny(type) && (targetType === globalObjectType || targetType === globalFunctionType)) {
return type;
}

if (!targetType) {
// Target type is type of construct signature
let constructSignatures: Signature[];
Expand Down Expand Up @@ -8615,14 +8616,20 @@ namespace ts {
}

function narrowTypeByTypePredicate(type: Type, callExpression: CallExpression, assumeTrue: boolean): Type {
if (type.flags & TypeFlags.Any || !hasMatchingArgument(callExpression, reference)) {
if (!hasMatchingArgument(callExpression, reference)) {
return type;
}
const signature = getResolvedSignature(callExpression);
const predicate = signature.typePredicate;
if (!predicate) {
return type;
}

// Don't narrow from 'any' if the predicate type is exactly 'Object' or 'Function'
if (isTypeAny(type) && (predicate.type === globalObjectType || predicate.type === globalFunctionType)) {
return type;
}

if (isIdentifierTypePredicate(predicate)) {
const predicateArgument = callExpression.arguments[predicate.parameterIndex];
if (predicateArgument) {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
tests/cases/conformance/types/any/narrowExceptionVariableInCatchClause.ts(11,17): error TS2339: Property 'doPanic' does not exist on type '{ type: "foo"; dontPanic(): any; }'.
tests/cases/conformance/types/any/narrowExceptionVariableInCatchClause.ts(16,17): error TS2339: Property 'massage' does not exist on type 'Error'.


==== tests/cases/conformance/types/any/narrowExceptionVariableInCatchClause.ts (2 errors) ====
declare function isFooError(x: any): x is { type: 'foo'; dontPanic(); };

function tryCatch() {
try {
// do stuff...
}
catch (err) { // err is implicitly 'any' and cannot be annotated

if (isFooError(err)) {
err.dontPanic(); // OK
err.doPanic(); // ERROR: Property 'doPanic' does not exist on type '{...}'
~~~~~~~
!!! error TS2339: Property 'doPanic' does not exist on type '{ type: "foo"; dontPanic(): any; }'.
}

else if (err instanceof Error) {
err.message;
err.massage; // ERROR: Property 'massage' does not exist on type 'Error'
~~~~~~~
!!! error TS2339: Property 'massage' does not exist on type 'Error'.
}

else {
throw err;
}
}
}

44 changes: 44 additions & 0 deletions tests/baselines/reference/narrowExceptionVariableInCatchClause.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
//// [narrowExceptionVariableInCatchClause.ts]
declare function isFooError(x: any): x is { type: 'foo'; dontPanic(); };

function tryCatch() {
try {
// do stuff...
}
catch (err) { // err is implicitly 'any' and cannot be annotated

if (isFooError(err)) {
err.dontPanic(); // OK
err.doPanic(); // ERROR: Property 'doPanic' does not exist on type '{...}'
}

else if (err instanceof Error) {
err.message;
err.massage; // ERROR: Property 'massage' does not exist on type 'Error'
}

else {
throw err;
}
}
}


//// [narrowExceptionVariableInCatchClause.js]
function tryCatch() {
try {
}
catch (err) {
if (isFooError(err)) {
err.dontPanic(); // OK
err.doPanic(); // ERROR: Property 'doPanic' does not exist on type '{...}'
}
else if (err instanceof Error) {
err.message;
err.massage; // ERROR: Property 'massage' does not exist on type 'Error'
}
else {
throw err;
}
}
}
33 changes: 33 additions & 0 deletions tests/baselines/reference/narrowFromAnyWithInstanceof.errors.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
tests/cases/conformance/types/any/narrowFromAnyWithInstanceof.ts(17,7): error TS2339: Property 'mesage' does not exist on type 'Error'.
tests/cases/conformance/types/any/narrowFromAnyWithInstanceof.ts(22,7): error TS2339: Property 'getHuors' does not exist on type 'Date'.


==== tests/cases/conformance/types/any/narrowFromAnyWithInstanceof.ts (2 errors) ====
declare var x: any;

if (x instanceof Function) { // 'any' is not narrowed when target type is 'Function'
x();
x(1, 2, 3);
x("hello!");
x.prop;
}

if (x instanceof Object) { // 'any' is not narrowed when target type is 'Object'
x.method();
x();
}

if (x instanceof Error) { // 'any' is narrowed to types other than 'Function'/'Object'
x.message;
x.mesage;
~~~~~~
!!! error TS2339: Property 'mesage' does not exist on type 'Error'.
}

if (x instanceof Date) {
x.getDate();
x.getHuors();
~~~~~~~~
!!! error TS2339: Property 'getHuors' does not exist on type 'Date'.
}

45 changes: 45 additions & 0 deletions tests/baselines/reference/narrowFromAnyWithInstanceof.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
//// [narrowFromAnyWithInstanceof.ts]
declare var x: any;

if (x instanceof Function) { // 'any' is not narrowed when target type is 'Function'
x();
x(1, 2, 3);
x("hello!");
x.prop;
}

if (x instanceof Object) { // 'any' is not narrowed when target type is 'Object'
x.method();
x();
}

if (x instanceof Error) { // 'any' is narrowed to types other than 'Function'/'Object'
x.message;
x.mesage;
}

if (x instanceof Date) {
x.getDate();
x.getHuors();
}


//// [narrowFromAnyWithInstanceof.js]
if (x instanceof Function) {
x();
x(1, 2, 3);
x("hello!");
x.prop;
}
if (x instanceof Object) {
x.method();
x();
}
if (x instanceof Error) {
x.message;
x.mesage;
}
if (x instanceof Date) {
x.getDate();
x.getHuors();
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
tests/cases/conformance/types/any/narrowFromAnyWithTypePredicate.ts(22,7): error TS2339: Property 'method' does not exist on type '{}'.
tests/cases/conformance/types/any/narrowFromAnyWithTypePredicate.ts(23,5): error TS2349: Cannot invoke an expression whose type lacks a call signature.
tests/cases/conformance/types/any/narrowFromAnyWithTypePredicate.ts(28,7): error TS2339: Property 'mesage' does not exist on type 'Error'.
tests/cases/conformance/types/any/narrowFromAnyWithTypePredicate.ts(33,7): error TS2339: Property 'getHuors' does not exist on type 'Date'.


==== tests/cases/conformance/types/any/narrowFromAnyWithTypePredicate.ts (4 errors) ====
declare var x: any;
declare function isFunction(x): x is Function;
declare function isObject(x): x is Object;
declare function isAnything(x): x is {};
declare function isError(x): x is Error;
declare function isDate(x): x is Date;


if (isFunction(x)) { // 'any' is not narrowed when target type is 'Function'
x();
x(1, 2, 3);
x("hello!");
x.prop;
}

if (isObject(x)) { // 'any' is not narrowed when target type is 'Object'
x.method();
x();
}

if (isAnything(x)) { // 'any' is narrowed to types other than 'Function'/'Object' (including {})
x.method();
~~~~~~
!!! error TS2339: Property 'method' does not exist on type '{}'.
x();
~~~
!!! error TS2349: Cannot invoke an expression whose type lacks a call signature.
}

if (isError(x)) {
x.message;
x.mesage;
~~~~~~
!!! error TS2339: Property 'mesage' does not exist on type 'Error'.
}

if (isDate(x)) {
x.getDate();
x.getHuors();
~~~~~~~~
!!! error TS2339: Property 'getHuors' does not exist on type 'Date'.
}

60 changes: 60 additions & 0 deletions tests/baselines/reference/narrowFromAnyWithTypePredicate.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
//// [narrowFromAnyWithTypePredicate.ts]
declare var x: any;
declare function isFunction(x): x is Function;
declare function isObject(x): x is Object;
declare function isAnything(x): x is {};
declare function isError(x): x is Error;
declare function isDate(x): x is Date;


if (isFunction(x)) { // 'any' is not narrowed when target type is 'Function'
x();
x(1, 2, 3);
x("hello!");
x.prop;
}

if (isObject(x)) { // 'any' is not narrowed when target type is 'Object'
x.method();
x();
}

if (isAnything(x)) { // 'any' is narrowed to types other than 'Function'/'Object' (including {})
x.method();
x();
}

if (isError(x)) {
x.message;
x.mesage;
}

if (isDate(x)) {
x.getDate();
x.getHuors();
}


//// [narrowFromAnyWithTypePredicate.js]
if (isFunction(x)) {
x();
x(1, 2, 3);
x("hello!");
x.prop;
}
if (isObject(x)) {
x.method();
x();
}
if (isAnything(x)) {
x.method();
x();
}
if (isError(x)) {
x.message;
x.mesage;
}
if (isDate(x)) {
x.getDate();
x.getHuors();
}
Loading