Skip to content

Commit

Permalink
Instantiating a signature in the context of another should infer from…
Browse files Browse the repository at this point in the history
… return type predicates if they match up (microsoft#30242)

* Instantiating a signature in the context of another should infer from return type predicates if they match up

* Invert condition per PR feedback
  • Loading branch information
weswigham authored Mar 8, 2019
1 parent 4c9ad08 commit e982240
Show file tree
Hide file tree
Showing 5 changed files with 153 additions and 0 deletions.
6 changes: 6 additions & 0 deletions src/compiler/checker.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20042,6 +20042,12 @@ namespace ts {
});
if (!contextualMapper) {
inferTypes(context.inferences, getReturnTypeOfSignature(contextualSignature), getReturnTypeOfSignature(signature), InferencePriority.ReturnType);
const signaturePredicate = getTypePredicateOfSignature(signature);
const contextualPredicate = getTypePredicateOfSignature(sourceSignature);
if (signaturePredicate && contextualPredicate && signaturePredicate.kind === contextualPredicate.kind &&
(signaturePredicate.kind === TypePredicateKind.This || signaturePredicate.parameterIndex === (contextualPredicate as IdentifierTypePredicate).parameterIndex)) {
inferTypes(context.inferences, contextualPredicate.type, signaturePredicate.type, InferencePriority.ReturnType);
}
}
return getSignatureInstantiation(signature, getInferredTypes(context), isInJSFile(contextualSignature.declaration));
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
//// [returnTypePredicateIsInstantiateInContextOfTarget.tsx]
/// <reference path="/.lib/react16.d.ts" />
import * as React from "react";
class TestComponent extends React.Component<{ isAny: <T>(obj: any) => obj is T }> {
static defaultProps = {
isAny: TestComponent.isAny
}

// Type guard is defined as a static class property
static isAny<T>(obj: any): obj is T {
return true;
}
}

const TestRender = () => <TestComponent />;

//// [returnTypePredicateIsInstantiateInContextOfTarget.js]
"use strict";
var __extends = (this && this.__extends) || (function () {
var extendStatics = function (d, b) {
extendStatics = Object.setPrototypeOf ||
({ __proto__: [] } instanceof Array && function (d, b) { d.__proto__ = b; }) ||
function (d, b) { for (var p in b) if (b.hasOwnProperty(p)) d[p] = b[p]; };
return extendStatics(d, b);
};
return function (d, b) {
extendStatics(d, b);
function __() { this.constructor = d; }
d.prototype = b === null ? Object.create(b) : (__.prototype = b.prototype, new __());
};
})();
exports.__esModule = true;
/// <reference path="react16.d.ts" />
var React = require("react");
var TestComponent = /** @class */ (function (_super) {
__extends(TestComponent, _super);
function TestComponent() {
return _super !== null && _super.apply(this, arguments) || this;
}
// Type guard is defined as a static class property
TestComponent.isAny = function (obj) {
return true;
};
TestComponent.defaultProps = {
isAny: TestComponent.isAny
};
return TestComponent;
}(React.Component));
var TestRender = function () { return React.createElement(TestComponent, null); };
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
=== tests/cases/compiler/returnTypePredicateIsInstantiateInContextOfTarget.tsx ===
/// <reference path="react16.d.ts" />
import * as React from "react";
>React : Symbol(React, Decl(returnTypePredicateIsInstantiateInContextOfTarget.tsx, 1, 6))

class TestComponent extends React.Component<{ isAny: <T>(obj: any) => obj is T }> {
>TestComponent : Symbol(TestComponent, Decl(returnTypePredicateIsInstantiateInContextOfTarget.tsx, 1, 31))
>React.Component : Symbol(React.Component, Decl(react16.d.ts, 345, 54), Decl(react16.d.ts, 349, 94))
>React : Symbol(React, Decl(returnTypePredicateIsInstantiateInContextOfTarget.tsx, 1, 6))
>Component : Symbol(React.Component, Decl(react16.d.ts, 345, 54), Decl(react16.d.ts, 349, 94))
>isAny : Symbol(isAny, Decl(returnTypePredicateIsInstantiateInContextOfTarget.tsx, 2, 45))
>T : Symbol(T, Decl(returnTypePredicateIsInstantiateInContextOfTarget.tsx, 2, 54))
>obj : Symbol(obj, Decl(returnTypePredicateIsInstantiateInContextOfTarget.tsx, 2, 57))
>obj : Symbol(obj, Decl(returnTypePredicateIsInstantiateInContextOfTarget.tsx, 2, 57))
>T : Symbol(T, Decl(returnTypePredicateIsInstantiateInContextOfTarget.tsx, 2, 54))

static defaultProps = {
>defaultProps : Symbol(TestComponent.defaultProps, Decl(returnTypePredicateIsInstantiateInContextOfTarget.tsx, 2, 83))

isAny: TestComponent.isAny
>isAny : Symbol(isAny, Decl(returnTypePredicateIsInstantiateInContextOfTarget.tsx, 3, 27))
>TestComponent.isAny : Symbol(TestComponent.isAny, Decl(returnTypePredicateIsInstantiateInContextOfTarget.tsx, 5, 5))
>TestComponent : Symbol(TestComponent, Decl(returnTypePredicateIsInstantiateInContextOfTarget.tsx, 1, 31))
>isAny : Symbol(TestComponent.isAny, Decl(returnTypePredicateIsInstantiateInContextOfTarget.tsx, 5, 5))
}

// Type guard is defined as a static class property
static isAny<T>(obj: any): obj is T {
>isAny : Symbol(TestComponent.isAny, Decl(returnTypePredicateIsInstantiateInContextOfTarget.tsx, 5, 5))
>T : Symbol(T, Decl(returnTypePredicateIsInstantiateInContextOfTarget.tsx, 8, 17))
>obj : Symbol(obj, Decl(returnTypePredicateIsInstantiateInContextOfTarget.tsx, 8, 20))
>obj : Symbol(obj, Decl(returnTypePredicateIsInstantiateInContextOfTarget.tsx, 8, 20))
>T : Symbol(T, Decl(returnTypePredicateIsInstantiateInContextOfTarget.tsx, 8, 17))

return true;
}
}

const TestRender = () => <TestComponent />;
>TestRender : Symbol(TestRender, Decl(returnTypePredicateIsInstantiateInContextOfTarget.tsx, 13, 5))
>TestComponent : Symbol(TestComponent, Decl(returnTypePredicateIsInstantiateInContextOfTarget.tsx, 1, 31))

Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
=== tests/cases/compiler/returnTypePredicateIsInstantiateInContextOfTarget.tsx ===
/// <reference path="react16.d.ts" />
import * as React from "react";
>React : typeof React

class TestComponent extends React.Component<{ isAny: <T>(obj: any) => obj is T }> {
>TestComponent : TestComponent
>React.Component : React.Component<{ isAny: <T>(obj: any) => obj is T; }, {}, any>
>React : typeof React
>Component : typeof React.Component
>isAny : <T>(obj: any) => obj is T
>obj : any

static defaultProps = {
>defaultProps : { isAny: <T>(obj: any) => obj is T; }
>{ isAny: TestComponent.isAny } : { isAny: <T>(obj: any) => obj is T; }

isAny: TestComponent.isAny
>isAny : <T>(obj: any) => obj is T
>TestComponent.isAny : <T>(obj: any) => obj is T
>TestComponent : typeof TestComponent
>isAny : <T>(obj: any) => obj is T
}

// Type guard is defined as a static class property
static isAny<T>(obj: any): obj is T {
>isAny : <T>(obj: any) => obj is T
>obj : any

return true;
>true : true
}
}

const TestRender = () => <TestComponent />;
>TestRender : () => JSX.Element
>() => <TestComponent /> : () => JSX.Element
><TestComponent /> : JSX.Element
>TestComponent : typeof TestComponent

Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
// @jsx: react
// @strict: true
/// <reference path="/.lib/react16.d.ts" />
import * as React from "react";
class TestComponent extends React.Component<{ isAny: <T>(obj: any) => obj is T }> {
static defaultProps = {
isAny: TestComponent.isAny
}

// Type guard is defined as a static class property
static isAny<T>(obj: any): obj is T {
return true;
}
}

const TestRender = () => <TestComponent />;

0 comments on commit e982240

Please sign in to comment.