Skip to content

Commit d351610

Browse files
authored
Update opposite direction of #28218 (#28384)
1 parent d7390c0 commit d351610

File tree

5 files changed

+134
-3
lines changed

5 files changed

+134
-3
lines changed

src/compiler/checker.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -12035,8 +12035,8 @@ namespace ts {
1203512035
}
1203612036

1203712037
if (target.flags & TypeFlags.TypeParameter) {
12038-
// A source type { [P in keyof T]: X } is related to a target type T if X is related to T[P].
12039-
if (getObjectFlags(source) & ObjectFlags.Mapped && getConstraintTypeFromMappedType(<MappedType>source) === getIndexType(target)) {
12038+
// A source type { [P in Q]: X } is related to a target type T if keyof T is related to Q and X is related to T[Q].
12039+
if (getObjectFlags(source) & ObjectFlags.Mapped && isRelatedTo(getIndexType(target), getConstraintTypeFromMappedType(<MappedType>source))) {
1204012040
if (!(getMappedTypeModifiers(<MappedType>source) & MappedTypeModifiers.IncludeOptional)) {
1204112041
const templateType = getTemplateTypeFromMappedType(<MappedType>source);
1204212042
const indexedAccessType = getIndexedAccessType(target, getTypeParameterFromMappedType(<MappedType>source));
@@ -12090,7 +12090,7 @@ namespace ts {
1209012090
(<IndexedAccessType>template).indexType === getTypeParameterFromMappedType(target)) {
1209112091
return Ternary.True;
1209212092
}
12093-
// A source type T is related to a target type { [P in keyof T]: X } if T[P] is related to X.
12093+
// A source type T is related to a target type { [P in Q]: X } if Q is related to keyof T and T[Q] is related to X.
1209412094
if (!isGenericMappedType(source) && isRelatedTo(getConstraintTypeFromMappedType(target), getIndexType(source))) {
1209512095
const indexedAccessType = getIndexedAccessType(source, getTypeParameterFromMappedType(target));
1209612096
const templateType = getTemplateTypeFromMappedType(target);
Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
//// [reactHOCSpreadprops.tsx]
2+
/// <reference path="/.lib/react16.d.ts" />
3+
import React = require("react");
4+
function f<P>(App: React.ComponentClass<P> | React.StatelessComponent<P>): void {
5+
class C extends React.Component<P & { x: number }> {
6+
render() {
7+
return <App {...this.props} />;
8+
}
9+
}
10+
}
11+
12+
13+
//// [reactHOCSpreadprops.js]
14+
"use strict";
15+
var __extends = (this && this.__extends) || (function () {
16+
var extendStatics = function (d, b) {
17+
extendStatics = Object.setPrototypeOf ||
18+
({ __proto__: [] } instanceof Array && function (d, b) { d.__proto__ = b; }) ||
19+
function (d, b) { for (var p in b) if (b.hasOwnProperty(p)) d[p] = b[p]; };
20+
return extendStatics(d, b);
21+
};
22+
return function (d, b) {
23+
extendStatics(d, b);
24+
function __() { this.constructor = d; }
25+
d.prototype = b === null ? Object.create(b) : (__.prototype = b.prototype, new __());
26+
};
27+
})();
28+
var __assign = (this && this.__assign) || function () {
29+
__assign = Object.assign || function(t) {
30+
for (var s, i = 1, n = arguments.length; i < n; i++) {
31+
s = arguments[i];
32+
for (var p in s) if (Object.prototype.hasOwnProperty.call(s, p))
33+
t[p] = s[p];
34+
}
35+
return t;
36+
};
37+
return __assign.apply(this, arguments);
38+
};
39+
exports.__esModule = true;
40+
/// <reference path="react16.d.ts" />
41+
var React = require("react");
42+
function f(App) {
43+
var C = /** @class */ (function (_super) {
44+
__extends(C, _super);
45+
function C() {
46+
return _super !== null && _super.apply(this, arguments) || this;
47+
}
48+
C.prototype.render = function () {
49+
return React.createElement(App, __assign({}, this.props));
50+
};
51+
return C;
52+
}(React.Component));
53+
}
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
=== tests/cases/compiler/reactHOCSpreadprops.tsx ===
2+
/// <reference path="react16.d.ts" />
3+
import React = require("react");
4+
>React : Symbol(React, Decl(reactHOCSpreadprops.tsx, 0, 0))
5+
6+
function f<P>(App: React.ComponentClass<P> | React.StatelessComponent<P>): void {
7+
>f : Symbol(f, Decl(reactHOCSpreadprops.tsx, 1, 32))
8+
>P : Symbol(P, Decl(reactHOCSpreadprops.tsx, 2, 11))
9+
>App : Symbol(App, Decl(reactHOCSpreadprops.tsx, 2, 14))
10+
>React : Symbol(React, Decl(reactHOCSpreadprops.tsx, 0, 0))
11+
>ComponentClass : Symbol(React.ComponentClass, Decl(react16.d.ts, 421, 9))
12+
>P : Symbol(P, Decl(reactHOCSpreadprops.tsx, 2, 11))
13+
>React : Symbol(React, Decl(reactHOCSpreadprops.tsx, 0, 0))
14+
>StatelessComponent : Symbol(React.StatelessComponent, Decl(react16.d.ts, 406, 49))
15+
>P : Symbol(P, Decl(reactHOCSpreadprops.tsx, 2, 11))
16+
17+
class C extends React.Component<P & { x: number }> {
18+
>C : Symbol(C, Decl(reactHOCSpreadprops.tsx, 2, 81))
19+
>React.Component : Symbol(React.Component, Decl(react16.d.ts, 345, 54), Decl(react16.d.ts, 349, 94))
20+
>React : Symbol(React, Decl(reactHOCSpreadprops.tsx, 0, 0))
21+
>Component : Symbol(React.Component, Decl(react16.d.ts, 345, 54), Decl(react16.d.ts, 349, 94))
22+
>P : Symbol(P, Decl(reactHOCSpreadprops.tsx, 2, 11))
23+
>x : Symbol(x, Decl(reactHOCSpreadprops.tsx, 3, 41))
24+
25+
render() {
26+
>render : Symbol(C.render, Decl(reactHOCSpreadprops.tsx, 3, 56))
27+
28+
return <App {...this.props} />;
29+
>App : Symbol(App, Decl(reactHOCSpreadprops.tsx, 2, 14))
30+
>this.props : Symbol(React.Component.props, Decl(react16.d.ts, 367, 32))
31+
>this : Symbol(C, Decl(reactHOCSpreadprops.tsx, 2, 81))
32+
>props : Symbol(React.Component.props, Decl(react16.d.ts, 367, 32))
33+
}
34+
}
35+
}
36+
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
=== tests/cases/compiler/reactHOCSpreadprops.tsx ===
2+
/// <reference path="react16.d.ts" />
3+
import React = require("react");
4+
>React : typeof React
5+
6+
function f<P>(App: React.ComponentClass<P> | React.StatelessComponent<P>): void {
7+
>f : <P>(App: React.ComponentClass<P, any> | React.StatelessComponent<P>) => void
8+
>App : React.ComponentClass<P, any> | React.StatelessComponent<P>
9+
>React : any
10+
>React : any
11+
12+
class C extends React.Component<P & { x: number }> {
13+
>C : C
14+
>React.Component : React.Component<P & { x: number; }, {}, any>
15+
>React : typeof React
16+
>Component : typeof React.Component
17+
>x : number
18+
19+
render() {
20+
>render : () => JSX.Element
21+
22+
return <App {...this.props} />;
23+
><App {...this.props} /> : JSX.Element
24+
>App : React.ComponentClass<P, any> | React.StatelessComponent<P>
25+
>this.props : Readonly<{ children?: React.ReactNode; }> & Readonly<P & { x: number; }>
26+
>this : this
27+
>props : Readonly<{ children?: React.ReactNode; }> & Readonly<P & { x: number; }>
28+
}
29+
}
30+
}
31+
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
// @jsx: react
2+
// @strict: true
3+
/// <reference path="/.lib/react16.d.ts" />
4+
import React = require("react");
5+
function f<P>(App: React.ComponentClass<P> | React.StatelessComponent<P>): void {
6+
class C extends React.Component<P & { x: number }> {
7+
render() {
8+
return <App {...this.props} />;
9+
}
10+
}
11+
}

0 commit comments

Comments
 (0)