Skip to content

Commit ab32fca

Browse files
authored
Fix type parameter lookup for TypeAlias LibraryManagedAttributes (#42245)
1 parent d1e1e9d commit ab32fca

File tree

5 files changed

+193
-5
lines changed

5 files changed

+193
-5
lines changed

src/compiler/checker.ts

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -24771,16 +24771,19 @@ namespace ts {
2477124771
function getJsxManagedAttributesFromLocatedAttributes(context: JsxOpeningLikeElement, ns: Symbol, attributesType: Type) {
2477224772
const managedSym = getJsxLibraryManagedAttributes(ns);
2477324773
if (managedSym) {
24774-
const declaredManagedType = getDeclaredTypeOfSymbol(managedSym);
24774+
const declaredManagedType = getDeclaredTypeOfSymbol(managedSym); // fetches interface type, or initializes symbol links type parmaeters
2477524775
const ctorType = getStaticTypeOfReferencedJsxConstructor(context);
24776+
if (managedSym.flags & SymbolFlags.TypeAlias) {
24777+
const params = getSymbolLinks(managedSym).typeParameters;
24778+
if (length(params) >= 2) {
24779+
const args = fillMissingTypeArguments([ctorType, attributesType], params, 2, isInJSFile(context));
24780+
return getTypeAliasInstantiation(managedSym, args);
24781+
}
24782+
}
2477624783
if (length((declaredManagedType as GenericType).typeParameters) >= 2) {
2477724784
const args = fillMissingTypeArguments([ctorType, attributesType], (declaredManagedType as GenericType).typeParameters, 2, isInJSFile(context));
2477824785
return createTypeReference((declaredManagedType as GenericType), args);
2477924786
}
24780-
else if (length(declaredManagedType.aliasTypeArguments) >= 2) {
24781-
const args = fillMissingTypeArguments([ctorType, attributesType], declaredManagedType.aliasTypeArguments, 2, isInJSFile(context));
24782-
return getTypeAliasInstantiation(declaredManagedType.aliasSymbol!, args);
24783-
}
2478424787
}
2478524788
return attributesType;
2478624789
}
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
//// [jsxLibraryManagedAttributesUnusedGeneric.tsx]
2+
// @ts-ignore
3+
import React from 'react'
4+
5+
declare const jsx: typeof React.createElement
6+
namespace jsx {
7+
export namespace JSX {
8+
export interface Element {}
9+
export interface ElementClass {}
10+
export interface ElementAttributesProperty {}
11+
export interface ElementChildrenAttribute {}
12+
export interface IntrinsicAttributes {}
13+
export interface IntrinsicClassAttributes<T> {}
14+
export type IntrinsicElements = {
15+
div: { className: string }
16+
}
17+
// Works
18+
// export type LibraryManagedAttributes<C, P> = P & { css: string };
19+
20+
// Equivalent to above, but fails
21+
export type WithCSSProp<P> = P & { css: string }
22+
export type LibraryManagedAttributes<C, P> = WithCSSProp<P>
23+
24+
}
25+
}
26+
27+
declare const Comp: (p: { className?: string }) => null
28+
29+
;<Comp css="color:hotpink;" />
30+
31+
//// [jsxLibraryManagedAttributesUnusedGeneric.js]
32+
"use strict";
33+
exports.__esModule = true;
34+
jsx(Comp, { css: "color:hotpink;" });
Lines changed: 70 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,70 @@
1+
=== tests/cases/compiler/jsxLibraryManagedAttributesUnusedGeneric.tsx ===
2+
// @ts-ignore
3+
import React from 'react'
4+
>React : Symbol(React, Decl(jsxLibraryManagedAttributesUnusedGeneric.tsx, 1, 6))
5+
6+
declare const jsx: typeof React.createElement
7+
>jsx : Symbol(jsx, Decl(jsxLibraryManagedAttributesUnusedGeneric.tsx, 3, 13), Decl(jsxLibraryManagedAttributesUnusedGeneric.tsx, 3, 45))
8+
>React : Symbol(React, Decl(jsxLibraryManagedAttributesUnusedGeneric.tsx, 1, 6))
9+
10+
namespace jsx {
11+
>jsx : Symbol(jsx, Decl(jsxLibraryManagedAttributesUnusedGeneric.tsx, 3, 13), Decl(jsxLibraryManagedAttributesUnusedGeneric.tsx, 3, 45))
12+
13+
export namespace JSX {
14+
>JSX : Symbol(JSX, Decl(jsxLibraryManagedAttributesUnusedGeneric.tsx, 4, 15))
15+
16+
export interface Element {}
17+
>Element : Symbol(Element, Decl(jsxLibraryManagedAttributesUnusedGeneric.tsx, 5, 26))
18+
19+
export interface ElementClass {}
20+
>ElementClass : Symbol(ElementClass, Decl(jsxLibraryManagedAttributesUnusedGeneric.tsx, 6, 35))
21+
22+
export interface ElementAttributesProperty {}
23+
>ElementAttributesProperty : Symbol(ElementAttributesProperty, Decl(jsxLibraryManagedAttributesUnusedGeneric.tsx, 7, 40))
24+
25+
export interface ElementChildrenAttribute {}
26+
>ElementChildrenAttribute : Symbol(ElementChildrenAttribute, Decl(jsxLibraryManagedAttributesUnusedGeneric.tsx, 8, 53))
27+
28+
export interface IntrinsicAttributes {}
29+
>IntrinsicAttributes : Symbol(IntrinsicAttributes, Decl(jsxLibraryManagedAttributesUnusedGeneric.tsx, 9, 52))
30+
31+
export interface IntrinsicClassAttributes<T> {}
32+
>IntrinsicClassAttributes : Symbol(IntrinsicClassAttributes, Decl(jsxLibraryManagedAttributesUnusedGeneric.tsx, 10, 47))
33+
>T : Symbol(T, Decl(jsxLibraryManagedAttributesUnusedGeneric.tsx, 11, 50))
34+
35+
export type IntrinsicElements = {
36+
>IntrinsicElements : Symbol(IntrinsicElements, Decl(jsxLibraryManagedAttributesUnusedGeneric.tsx, 11, 55))
37+
38+
div: { className: string }
39+
>div : Symbol(div, Decl(jsxLibraryManagedAttributesUnusedGeneric.tsx, 12, 41))
40+
>className : Symbol(className, Decl(jsxLibraryManagedAttributesUnusedGeneric.tsx, 13, 18))
41+
}
42+
// Works
43+
// export type LibraryManagedAttributes<C, P> = P & { css: string };
44+
45+
// Equivalent to above, but fails
46+
export type WithCSSProp<P> = P & { css: string }
47+
>WithCSSProp : Symbol(WithCSSProp, Decl(jsxLibraryManagedAttributesUnusedGeneric.tsx, 14, 9))
48+
>P : Symbol(P, Decl(jsxLibraryManagedAttributesUnusedGeneric.tsx, 19, 32))
49+
>P : Symbol(P, Decl(jsxLibraryManagedAttributesUnusedGeneric.tsx, 19, 32))
50+
>css : Symbol(css, Decl(jsxLibraryManagedAttributesUnusedGeneric.tsx, 19, 42))
51+
52+
export type LibraryManagedAttributes<C, P> = WithCSSProp<P>
53+
>LibraryManagedAttributes : Symbol(LibraryManagedAttributes, Decl(jsxLibraryManagedAttributesUnusedGeneric.tsx, 19, 56))
54+
>C : Symbol(C, Decl(jsxLibraryManagedAttributesUnusedGeneric.tsx, 20, 45))
55+
>P : Symbol(P, Decl(jsxLibraryManagedAttributesUnusedGeneric.tsx, 20, 47))
56+
>WithCSSProp : Symbol(WithCSSProp, Decl(jsxLibraryManagedAttributesUnusedGeneric.tsx, 14, 9))
57+
>P : Symbol(P, Decl(jsxLibraryManagedAttributesUnusedGeneric.tsx, 20, 47))
58+
59+
}
60+
}
61+
62+
declare const Comp: (p: { className?: string }) => null
63+
>Comp : Symbol(Comp, Decl(jsxLibraryManagedAttributesUnusedGeneric.tsx, 25, 13))
64+
>p : Symbol(p, Decl(jsxLibraryManagedAttributesUnusedGeneric.tsx, 25, 21))
65+
>className : Symbol(className, Decl(jsxLibraryManagedAttributesUnusedGeneric.tsx, 25, 25))
66+
67+
;<Comp css="color:hotpink;" />
68+
>Comp : Symbol(Comp, Decl(jsxLibraryManagedAttributesUnusedGeneric.tsx, 25, 13))
69+
>css : Symbol(css, Decl(jsxLibraryManagedAttributesUnusedGeneric.tsx, 27, 6))
70+
Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
=== tests/cases/compiler/jsxLibraryManagedAttributesUnusedGeneric.tsx ===
2+
// @ts-ignore
3+
import React from 'react'
4+
>React : any
5+
6+
declare const jsx: typeof React.createElement
7+
>jsx : error
8+
>React.createElement : error
9+
>React : any
10+
>createElement : any
11+
12+
namespace jsx {
13+
export namespace JSX {
14+
export interface Element {}
15+
export interface ElementClass {}
16+
export interface ElementAttributesProperty {}
17+
export interface ElementChildrenAttribute {}
18+
export interface IntrinsicAttributes {}
19+
export interface IntrinsicClassAttributes<T> {}
20+
export type IntrinsicElements = {
21+
>IntrinsicElements : IntrinsicElements
22+
23+
div: { className: string }
24+
>div : { className: string; }
25+
>className : string
26+
}
27+
// Works
28+
// export type LibraryManagedAttributes<C, P> = P & { css: string };
29+
30+
// Equivalent to above, but fails
31+
export type WithCSSProp<P> = P & { css: string }
32+
>WithCSSProp : WithCSSProp<P>
33+
>css : string
34+
35+
export type LibraryManagedAttributes<C, P> = WithCSSProp<P>
36+
>LibraryManagedAttributes : WithCSSProp<P>
37+
38+
}
39+
}
40+
41+
declare const Comp: (p: { className?: string }) => null
42+
>Comp : (p: { className?: string;}) => null
43+
>p : { className?: string; }
44+
>className : string
45+
>null : null
46+
47+
;<Comp css="color:hotpink;" />
48+
><Comp css="color:hotpink;" /> : jsx.JSX.Element
49+
>Comp : (p: { className?: string; }) => null
50+
>css : string
51+
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
// @jsx: react
2+
// @jsxFactory: jsx
3+
// @ts-ignore
4+
import React from 'react'
5+
6+
declare const jsx: typeof React.createElement
7+
namespace jsx {
8+
export namespace JSX {
9+
export interface Element {}
10+
export interface ElementClass {}
11+
export interface ElementAttributesProperty {}
12+
export interface ElementChildrenAttribute {}
13+
export interface IntrinsicAttributes {}
14+
export interface IntrinsicClassAttributes<T> {}
15+
export type IntrinsicElements = {
16+
div: { className: string }
17+
}
18+
// Works
19+
// export type LibraryManagedAttributes<C, P> = P & { css: string };
20+
21+
// Equivalent to above, but fails
22+
export type WithCSSProp<P> = P & { css: string }
23+
export type LibraryManagedAttributes<C, P> = WithCSSProp<P>
24+
25+
}
26+
}
27+
28+
declare const Comp: (p: { className?: string }) => null
29+
30+
;<Comp css="color:hotpink;" />

0 commit comments

Comments
 (0)