Skip to content

Don't crash if there's no JSX.Element during SFC resolution #7308

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

Merged
merged 1 commit into from
Mar 1, 2016
Merged
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
24 changes: 13 additions & 11 deletions src/compiler/checker.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8797,18 +8797,20 @@ namespace ts {
if (!elemClassType || !isTypeAssignableTo(elemInstanceType, elemClassType)) {
// Is this is a stateless function component? See if its single signature's return type is
// assignable to the JSX Element Type
const elemType = checkExpression(node.tagName);
const callSignatures = elemType && getSignaturesOfType(elemType, SignatureKind.Call);
const callSignature = callSignatures && callSignatures.length > 0 && callSignatures[0];
const callReturnType = callSignature && getReturnTypeOfSignature(callSignature);
let paramType = callReturnType && (callSignature.parameters.length === 0 ? emptyObjectType : getTypeOfSymbol(callSignature.parameters[0]));
if (callReturnType && isTypeAssignableTo(callReturnType, jsxElementType)) {
// Intersect in JSX.IntrinsicAttributes if it exists
const intrinsicAttributes = getJsxType(JsxNames.IntrinsicAttributes);
if (intrinsicAttributes !== unknownType) {
paramType = intersectTypes(intrinsicAttributes, paramType);
if (jsxElementType) {
const elemType = checkExpression(node.tagName);
const callSignatures = elemType && getSignaturesOfType(elemType, SignatureKind.Call);
const callSignature = callSignatures && callSignatures.length > 0 && callSignatures[0];
const callReturnType = callSignature && getReturnTypeOfSignature(callSignature);
let paramType = callReturnType && (callSignature.parameters.length === 0 ? emptyObjectType : getTypeOfSymbol(callSignature.parameters[0]));
if (callReturnType && isTypeAssignableTo(callReturnType, jsxElementType)) {
// Intersect in JSX.IntrinsicAttributes if it exists
const intrinsicAttributes = getJsxType(JsxNames.IntrinsicAttributes);
if (intrinsicAttributes !== unknownType) {
paramType = intersectTypes(intrinsicAttributes, paramType);
}
return links.resolvedJsxType = paramType;
}
return links.resolvedJsxType = paramType;
}
}

Expand Down
Original file line number Diff line number Diff line change
@@ -1,15 +1,18 @@
tests/cases/conformance/types/contextualTypes/jsxAttributes/contextuallyTypedStringLiteralsInJsxAttributes01.tsx(13,15): error TS2322: Type '"f"' is not assignable to type '"A" | "B" | "C"'.
tests/cases/conformance/types/contextualTypes/jsxAttributes/contextuallyTypedStringLiteralsInJsxAttributes01.tsx(16,15): error TS2322: Type '"f"' is not assignable to type '"A" | "B" | "C"'.
Type '"f"' is not assignable to type '"C"'.
tests/cases/conformance/types/contextualTypes/jsxAttributes/contextuallyTypedStringLiteralsInJsxAttributes01.tsx(14,15): error TS2322: Type '"f"' is not assignable to type '"A" | "B" | "C"'.
tests/cases/conformance/types/contextualTypes/jsxAttributes/contextuallyTypedStringLiteralsInJsxAttributes01.tsx(17,15): error TS2322: Type '"f"' is not assignable to type '"A" | "B" | "C"'.
Type '"f"' is not assignable to type '"C"'.


==== tests/cases/conformance/types/contextualTypes/jsxAttributes/contextuallyTypedStringLiteralsInJsxAttributes01.tsx (2 errors) ====

namespace JSX {
interface IntrinsicElements {
export interface IntrinsicElements {
span: {};
}
export interface Element {
something?: any;
}
}

const FooComponent = (props: { foo: "A" | "B" | "C" }) => <span>{props.foo}</span>;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,9 +1,12 @@
//// [contextuallyTypedStringLiteralsInJsxAttributes01.tsx]

namespace JSX {
interface IntrinsicElements {
export interface IntrinsicElements {
span: {};
}
export interface Element {
something?: any;
}
}

const FooComponent = (props: { foo: "A" | "B" | "C" }) => <span>{props.foo}</span>;
Expand All @@ -24,7 +27,13 @@ var FooComponent = function (props) { return <span>{props.foo}</span>; };

//// [contextuallyTypedStringLiteralsInJsxAttributes01.d.ts]
declare namespace JSX {
interface IntrinsicElements {
span: {};
}
interface Element {
something?: any;
}
}
declare const FooComponent: (props: {
foo: "A" | "B" | "C";
}) => any;
}) => JSX.Element;
9 changes: 9 additions & 0 deletions tests/baselines/reference/tsxAttributeResolution13.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
//// [test.tsx]

function Test() { }
<Test></Test>


//// [test.jsx]
function Test() { }
<Test></Test>;
9 changes: 9 additions & 0 deletions tests/baselines/reference/tsxAttributeResolution13.symbols
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
=== tests/cases/conformance/jsx/test.tsx ===

function Test() { }
>Test : Symbol(Test, Decl(test.tsx, 0, 0))

<Test></Test>
>Test : Symbol(Test, Decl(test.tsx, 0, 0))
>Test : Symbol(Test, Decl(test.tsx, 0, 0))

10 changes: 10 additions & 0 deletions tests/baselines/reference/tsxAttributeResolution13.types
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
=== tests/cases/conformance/jsx/test.tsx ===

function Test() { }
>Test : () => void

<Test></Test>
><Test></Test> : any
>Test : any
>Test : any

5 changes: 5 additions & 0 deletions tests/cases/conformance/jsx/tsxAttributeResolution13.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
//@jsx: preserve

//@filename: test.tsx
function Test() { }
<Test></Test>
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,12 @@
// @declaration: true

namespace JSX {
interface IntrinsicElements {
export interface IntrinsicElements {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why does this change to export and why add export interface Element? Does this somehow make them undefined for this test?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The test was invalid because IntrinsicElements wasn't actually exposed to the code below

span: {};
}
export interface Element {
something?: any;
}
}

const FooComponent = (props: { foo: "A" | "B" | "C" }) => <span>{props.foo}</span>;
Expand Down