Skip to content

Commit ca1d19a

Browse files
authored
Always make a new jsxAttributes type, dont reuse emptyObjectType (microsoft#22810)
* Always make a new jsxAttributes type, dont reuse emptyObjectType * Break up conditional a bit
1 parent a909000 commit ca1d19a

6 files changed

+143
-1
lines changed

src/compiler/checker.ts

+4-1
Original file line numberDiff line numberDiff line change
@@ -15398,7 +15398,10 @@ namespace ts {
1539815398
if (hasSpreadAnyType) {
1539915399
return anyType;
1540015400
}
15401-
return typeToIntersect && spread !== emptyObjectType ? getIntersectionType([typeToIntersect, spread]) : (typeToIntersect || spread);
15401+
if (typeToIntersect && spread !== emptyObjectType) {
15402+
return getIntersectionType([typeToIntersect, spread]);
15403+
}
15404+
return typeToIntersect || (spread === emptyObjectType ? createJsxAttributesType() : spread);
1540215405

1540315406
/**
1540415407
* Create anonymous type from given attributes symbol table.

tests/baselines/reference/tsxAttributeResolution12.errors.txt

+2
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ tests/cases/conformance/jsx/file.tsx(25,10): error TS2322: Type '{}' is not assi
33
Property 'reqd' is missing in type '{}'.
44
tests/cases/conformance/jsx/file.tsx(28,10): error TS2322: Type '{}' is not assignable to type 'IntrinsicAttributes & { reqd: any; }'.
55
Type '{}' is not assignable to type '{ reqd: any; }'.
6+
Property 'reqd' is missing in type '{}'.
67

78

89
==== tests/cases/conformance/jsx/react.d.ts (0 errors) ====
@@ -54,5 +55,6 @@ tests/cases/conformance/jsx/file.tsx(28,10): error TS2322: Type '{}' is not assi
5455
~~~~~~~~~~~~~~~~
5556
!!! error TS2322: Type '{}' is not assignable to type 'IntrinsicAttributes & { reqd: any; }'.
5657
!!! error TS2322: Type '{}' is not assignable to type '{ reqd: any; }'.
58+
!!! error TS2322: Property 'reqd' is missing in type '{}'.
5759

5860

Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
//// [tsxAttributesHasInferrableIndex.tsx]
2+
type AttributeValue = number | string | Date | boolean;
3+
interface Attributes {
4+
[key: string]: AttributeValue;
5+
}
6+
function createElement(name: string, attributes: Attributes | undefined, ...contents: string[]) {
7+
return name;
8+
}
9+
namespace createElement.JSX {
10+
type Element = string;
11+
}
12+
13+
function Button(attributes: Attributes | undefined, contents: string[]) {
14+
return '';
15+
}
16+
const b = <Button></Button>
17+
18+
19+
//// [tsxAttributesHasInferrableIndex.js]
20+
"use strict";
21+
function createElement(name, attributes) {
22+
var contents = [];
23+
for (var _i = 2; _i < arguments.length; _i++) {
24+
contents[_i - 2] = arguments[_i];
25+
}
26+
return name;
27+
}
28+
function Button(attributes, contents) {
29+
return '';
30+
}
31+
var b = createElement(Button, null);
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
=== tests/cases/compiler/tsxAttributesHasInferrableIndex.tsx ===
2+
type AttributeValue = number | string | Date | boolean;
3+
>AttributeValue : Symbol(AttributeValue, Decl(tsxAttributesHasInferrableIndex.tsx, 0, 0))
4+
>Date : Symbol(Date, Decl(lib.d.ts, --, --), Decl(lib.d.ts, --, --), Decl(lib.d.ts, --, --))
5+
6+
interface Attributes {
7+
>Attributes : Symbol(Attributes, Decl(tsxAttributesHasInferrableIndex.tsx, 0, 55))
8+
9+
[key: string]: AttributeValue;
10+
>key : Symbol(key, Decl(tsxAttributesHasInferrableIndex.tsx, 2, 5))
11+
>AttributeValue : Symbol(AttributeValue, Decl(tsxAttributesHasInferrableIndex.tsx, 0, 0))
12+
}
13+
function createElement(name: string, attributes: Attributes | undefined, ...contents: string[]) {
14+
>createElement : Symbol(createElement, Decl(tsxAttributesHasInferrableIndex.tsx, 3, 1), Decl(tsxAttributesHasInferrableIndex.tsx, 6, 1))
15+
>name : Symbol(name, Decl(tsxAttributesHasInferrableIndex.tsx, 4, 23))
16+
>attributes : Symbol(attributes, Decl(tsxAttributesHasInferrableIndex.tsx, 4, 36))
17+
>Attributes : Symbol(Attributes, Decl(tsxAttributesHasInferrableIndex.tsx, 0, 55))
18+
>contents : Symbol(contents, Decl(tsxAttributesHasInferrableIndex.tsx, 4, 72))
19+
20+
return name;
21+
>name : Symbol(name, Decl(tsxAttributesHasInferrableIndex.tsx, 4, 23))
22+
}
23+
namespace createElement.JSX {
24+
>createElement : Symbol(createElement, Decl(tsxAttributesHasInferrableIndex.tsx, 3, 1), Decl(tsxAttributesHasInferrableIndex.tsx, 6, 1))
25+
>JSX : Symbol(JSX, Decl(tsxAttributesHasInferrableIndex.tsx, 7, 24))
26+
27+
type Element = string;
28+
>Element : Symbol(Element, Decl(tsxAttributesHasInferrableIndex.tsx, 7, 29))
29+
}
30+
31+
function Button(attributes: Attributes | undefined, contents: string[]) {
32+
>Button : Symbol(Button, Decl(tsxAttributesHasInferrableIndex.tsx, 9, 1))
33+
>attributes : Symbol(attributes, Decl(tsxAttributesHasInferrableIndex.tsx, 11, 16))
34+
>Attributes : Symbol(Attributes, Decl(tsxAttributesHasInferrableIndex.tsx, 0, 55))
35+
>contents : Symbol(contents, Decl(tsxAttributesHasInferrableIndex.tsx, 11, 51))
36+
37+
return '';
38+
}
39+
const b = <Button></Button>
40+
>b : Symbol(b, Decl(tsxAttributesHasInferrableIndex.tsx, 14, 5))
41+
>Button : Symbol(Button, Decl(tsxAttributesHasInferrableIndex.tsx, 9, 1))
42+
>Button : Symbol(Button, Decl(tsxAttributesHasInferrableIndex.tsx, 9, 1))
43+
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
=== tests/cases/compiler/tsxAttributesHasInferrableIndex.tsx ===
2+
type AttributeValue = number | string | Date | boolean;
3+
>AttributeValue : AttributeValue
4+
>Date : Date
5+
6+
interface Attributes {
7+
>Attributes : Attributes
8+
9+
[key: string]: AttributeValue;
10+
>key : string
11+
>AttributeValue : AttributeValue
12+
}
13+
function createElement(name: string, attributes: Attributes | undefined, ...contents: string[]) {
14+
>createElement : (name: string, attributes: Attributes | undefined, ...contents: string[]) => string
15+
>name : string
16+
>attributes : Attributes | undefined
17+
>Attributes : Attributes
18+
>contents : string[]
19+
20+
return name;
21+
>name : string
22+
}
23+
namespace createElement.JSX {
24+
>createElement : (name: string, attributes: Attributes | undefined, ...contents: string[]) => string
25+
>JSX : any
26+
27+
type Element = string;
28+
>Element : string
29+
}
30+
31+
function Button(attributes: Attributes | undefined, contents: string[]) {
32+
>Button : (attributes: Attributes | undefined, contents: string[]) => string
33+
>attributes : Attributes | undefined
34+
>Attributes : Attributes
35+
>contents : string[]
36+
37+
return '';
38+
>'' : ""
39+
}
40+
const b = <Button></Button>
41+
>b : any
42+
><Button></Button> : any
43+
>Button : (attributes: Attributes | undefined, contents: string[]) => string
44+
>Button : (attributes: Attributes | undefined, contents: string[]) => string
45+
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
// @strict: true
2+
// @jsx: react
3+
// @jsxFactory: createElement
4+
type AttributeValue = number | string | Date | boolean;
5+
interface Attributes {
6+
[key: string]: AttributeValue;
7+
}
8+
function createElement(name: string, attributes: Attributes | undefined, ...contents: string[]) {
9+
return name;
10+
}
11+
namespace createElement.JSX {
12+
type Element = string;
13+
}
14+
15+
function Button(attributes: Attributes | undefined, contents: string[]) {
16+
return '';
17+
}
18+
const b = <Button></Button>

0 commit comments

Comments
 (0)