Skip to content

[Master] Fix 11566 - Allow SFC to return null #14102

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

Closed
wants to merge 5 commits into from
Closed
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
26 changes: 17 additions & 9 deletions src/compiler/checker.ts
Original file line number Diff line number Diff line change
Expand Up @@ -344,7 +344,8 @@ namespace ts {
});
const typeofType = createTypeofType();

let jsxElementType: Type;
let getJsxElementType: () => Type;
Copy link
Contributor Author

Choose a reason for hiding this comment

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

I change this to use memoize so it is similar to how we handle other type (see below)....now to think about it, it may have additional cause of function call...

let getJsxStatelessElementType: () => Type;
let _jsxNamespace: string;
let _jsxFactoryEntity: EntityName;

Expand Down Expand Up @@ -12183,12 +12184,12 @@ namespace ts {
type.flags & TypeFlags.UnionOrIntersection && !forEach((<UnionOrIntersectionType>type).types, t => !isValidSpreadType(t)));
}

function checkJsxSelfClosingElement(node: JsxSelfClosingElement) {
function checkJsxSelfClosingElement(node: JsxSelfClosingElement): Type {
checkJsxOpeningLikeElement(node);
return jsxElementType || anyType;
return getJsxElementType() || anyType;
}

function checkJsxElement(node: JsxElement) {
function checkJsxElement(node: JsxElement): Type {
// Check attributes
checkJsxOpeningLikeElement(node.openingElement);

Expand All @@ -12215,7 +12216,7 @@ namespace ts {
}
}

return jsxElementType || anyType;
return getJsxElementType() || anyType;
}

/**
Expand Down Expand Up @@ -12454,13 +12455,14 @@ namespace ts {
function defaultTryGetJsxStatelessFunctionAttributesType(openingLikeElement: JsxOpeningLikeElement, elementType: Type, elemInstanceType: Type, elementClassType?: Type): Type {
Debug.assert(!(elementType.flags & TypeFlags.Union));
if (!elementClassType || !isTypeAssignableTo(elemInstanceType, elementClassType)) {
if (jsxElementType) {
const jsxStatelessElementType = getJsxStatelessElementType();
if (jsxStatelessElementType) {
// We don't call getResolvedSignature here because we have already resolve the type of JSX Element.
const callSignature = getResolvedJsxStatelessFunctionSignature(openingLikeElement, elementType, /*candidatesOutArray*/ undefined);
if (callSignature !== unknownSignature) {
const callReturnType = callSignature && getReturnTypeOfSignature(callSignature);
let paramType = callReturnType && (callSignature.parameters.length === 0 ? emptyObjectType : getTypeOfSymbol(callSignature.parameters[0]));
if (callReturnType && isTypeAssignableTo(callReturnType, jsxElementType)) {
if (callReturnType && isTypeAssignableTo(callReturnType, jsxStatelessElementType)) {
// Intersect in JSX.IntrinsicAttributes if it exists
const intrinsicAttributes = getJsxType(JsxNames.IntrinsicAttributes);
if (intrinsicAttributes !== unknownType) {
Expand Down Expand Up @@ -12488,6 +12490,7 @@ namespace ts {
Debug.assert(!(elementType.flags & TypeFlags.Union));
if (!elementClassType || !isTypeAssignableTo(elemInstanceType, elementClassType)) {
// Is this is a stateless function component? See if its single signature's return type is assignable to the JSX Element Type
const jsxElementType = getJsxElementType();
if (jsxElementType) {
// We don't call getResolvedSignature because here we have already resolve the type of JSX Element.
const candidatesOutArray: Signature[] = [];
Expand Down Expand Up @@ -12760,7 +12763,7 @@ namespace ts {
error(errorNode, Diagnostics.Cannot_use_JSX_unless_the_jsx_flag_is_provided);
}

if (jsxElementType === undefined) {
if (getJsxElementType() === undefined) {
if (compilerOptions.noImplicitAny) {
error(errorNode, Diagnostics.JSX_element_implicitly_has_type_any_because_the_global_type_JSX_Element_does_not_exist);
}
Expand Down Expand Up @@ -21638,7 +21641,8 @@ namespace ts {
globalBooleanType = getGlobalType("Boolean");
globalRegExpType = getGlobalType("RegExp");

jsxElementType = getExportedTypeFromNamespace("JSX", JsxNames.Element);
getJsxElementType = memoize(() => getExportedTypeFromNamespace("JSX", JsxNames.Element));
getJsxStatelessElementType = memoize(createJsxStatelessElementType);
getGlobalClassDecoratorType = memoize(() => getGlobalType("ClassDecorator"));
getGlobalPropertyDecoratorType = memoize(() => getGlobalType("PropertyDecorator"));
getGlobalMethodDecoratorType = memoize(() => getGlobalType("MethodDecorator"));
Expand Down Expand Up @@ -21719,6 +21723,10 @@ namespace ts {
return externalHelpersModule;
}

function createJsxStatelessElementType() {
const jsxElementType = getJsxElementType();
return jsxElementType ? getUnionType([jsxElementType, nullType]) : nullType;
}

function createInstantiatedPromiseLikeType(): ObjectType {
const promiseLikeType = getGlobalPromiseLikeType();
Expand Down
24 changes: 24 additions & 0 deletions tests/baselines/reference/tsxSFCReturnNull.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
//// [file.tsx]

import React = require('react');

const Foo = (props: any) => null;

function Greet(x: {name?: string}) {
return null;
}

const foo = <Foo />;
const G = <Greet />;

//// [file.jsx]
define(["require", "exports", "react"], function (require, exports, React) {
"use strict";
exports.__esModule = true;
var Foo = function (props) { return null; };
function Greet(x) {
return null;
}
var foo = <Foo />;
var G = <Greet />;
});
25 changes: 25 additions & 0 deletions tests/baselines/reference/tsxSFCReturnNull.symbols
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
=== tests/cases/conformance/jsx/file.tsx ===

import React = require('react');
>React : Symbol(React, Decl(file.tsx, 0, 0))

const Foo = (props: any) => null;
>Foo : Symbol(Foo, Decl(file.tsx, 3, 5))
>props : Symbol(props, Decl(file.tsx, 3, 13))

function Greet(x: {name?: string}) {
>Greet : Symbol(Greet, Decl(file.tsx, 3, 33))
>x : Symbol(x, Decl(file.tsx, 5, 15))
>name : Symbol(name, Decl(file.tsx, 5, 19))

return null;
}

const foo = <Foo />;
>foo : Symbol(foo, Decl(file.tsx, 9, 5))
>Foo : Symbol(Foo, Decl(file.tsx, 3, 5))

const G = <Greet />;
>G : Symbol(G, Decl(file.tsx, 10, 5))
>Greet : Symbol(Greet, Decl(file.tsx, 3, 33))

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

import React = require('react');
>React : typeof React

const Foo = (props: any) => null;
>Foo : (props: any) => any
>(props: any) => null : (props: any) => any
>props : any
>null : null

function Greet(x: {name?: string}) {
>Greet : (x: { name?: string; }) => any
>x : { name?: string; }
>name : string

return null;
>null : null
}

const foo = <Foo />;
>foo : JSX.Element
><Foo /> : JSX.Element
>Foo : (props: any) => any

const G = <Greet />;
>G : JSX.Element
><Greet /> : JSX.Element
>Greet : (x: { name?: string; }) => any

24 changes: 24 additions & 0 deletions tests/baselines/reference/tsxSFCReturnNullStrictNullChecks.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
//// [file.tsx]

import React = require('react');

const Foo = (props: any) => null;

function Greet(x: {name?: string}) {
return null;
}

const foo = <Foo />;
const G = <Greet />;

//// [file.jsx]
define(["require", "exports", "react"], function (require, exports, React) {
"use strict";
exports.__esModule = true;
var Foo = function (props) { return null; };
function Greet(x) {
return null;
}
var foo = <Foo />;
var G = <Greet />;
});
25 changes: 25 additions & 0 deletions tests/baselines/reference/tsxSFCReturnNullStrictNullChecks.symbols
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
=== tests/cases/conformance/jsx/file.tsx ===

import React = require('react');
>React : Symbol(React, Decl(file.tsx, 0, 0))

const Foo = (props: any) => null;
>Foo : Symbol(Foo, Decl(file.tsx, 3, 5))
>props : Symbol(props, Decl(file.tsx, 3, 13))

function Greet(x: {name?: string}) {
>Greet : Symbol(Greet, Decl(file.tsx, 3, 33))
>x : Symbol(x, Decl(file.tsx, 5, 15))
>name : Symbol(name, Decl(file.tsx, 5, 19))

return null;
}

const foo = <Foo />;
>foo : Symbol(foo, Decl(file.tsx, 9, 5))
>Foo : Symbol(Foo, Decl(file.tsx, 3, 5))

const G = <Greet />;
>G : Symbol(G, Decl(file.tsx, 10, 5))
>Greet : Symbol(Greet, Decl(file.tsx, 3, 33))

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

import React = require('react');
>React : typeof React

const Foo = (props: any) => null;
>Foo : (props: any) => null
>(props: any) => null : (props: any) => null
>props : any
>null : null

function Greet(x: {name?: string}) {
>Greet : (x: { name?: string | undefined; }) => null
>x : { name?: string | undefined; }
>name : string | undefined

return null;
>null : null
}

const foo = <Foo />;
>foo : JSX.Element
><Foo /> : JSX.Element
>Foo : (props: any) => null

const G = <Greet />;
>G : JSX.Element
><Greet /> : JSX.Element
>Greet : (x: { name?: string | undefined; }) => null

Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
tests/cases/conformance/jsx/file.tsx(10,13): error TS2605: JSX element type 'undefined' is not a constructor function for JSX elements.
tests/cases/conformance/jsx/file.tsx(11,11): error TS2605: JSX element type 'undefined' is not a constructor function for JSX elements.


==== tests/cases/conformance/jsx/file.tsx (2 errors) ====

import React = require('react');

const Foo = (props: any) => undefined;
function Greet(x: {name?: string}) {
return undefined;
}

// Error
const foo = <Foo />;
~~~~~~~
!!! error TS2605: JSX element type 'undefined' is not a constructor function for JSX elements.
const G = <Greet />;
~~~~~~~~~
!!! error TS2605: JSX element type 'undefined' is not a constructor function for JSX elements.
25 changes: 25 additions & 0 deletions tests/baselines/reference/tsxSFCReturnUndefinedStrictNullChecks.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
//// [file.tsx]

import React = require('react');

const Foo = (props: any) => undefined;
function Greet(x: {name?: string}) {
return undefined;
}

// Error
const foo = <Foo />;
const G = <Greet />;

//// [file.jsx]
define(["require", "exports", "react"], function (require, exports, React) {
"use strict";
exports.__esModule = true;
var Foo = function (props) { return undefined; };
function Greet(x) {
return undefined;
}
// Error
var foo = <Foo />;
var G = <Greet />;
});
16 changes: 16 additions & 0 deletions tests/cases/conformance/jsx/tsxSFCReturnNull.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
// @filename: file.tsx
// @jsx: preserve
// @module: amd
// @noLib: true
// @libFiles: react.d.ts,lib.d.ts

import React = require('react');

const Foo = (props: any) => null;

function Greet(x: {name?: string}) {
return null;
}

const foo = <Foo />;
const G = <Greet />;
17 changes: 17 additions & 0 deletions tests/cases/conformance/jsx/tsxSFCReturnNullStrictNullChecks.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
// @filename: file.tsx
// @jsx: preserve
// @module: amd
// @noLib: true
// @strictNullChecks: true
// @libFiles: react.d.ts,lib.d.ts

import React = require('react');

const Foo = (props: any) => null;

function Greet(x: {name?: string}) {
return null;
}

const foo = <Foo />;
const G = <Greet />;
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
// @filename: file.tsx
// @jsx: preserve
// @module: amd
// @noLib: true
// @strictNullChecks: true
// @libFiles: react.d.ts,lib.d.ts

import React = require('react');

const Foo = (props: any) => undefined;
function Greet(x: {name?: string}) {
return undefined;
}

// Error
const foo = <Foo />;
const G = <Greet />;