Closed
Description
TypeScript Version: 3.3.0-dev.20181129
Code
ex.tsx:
import * as React from 'react';
import {BrowserRouter, HashRouter, Redirect, Route, RouteProps, Switch} from 'react-router-dom';
type State = {SelectedRouter?: typeof BrowserRouter | typeof HashRouter};
export class Example extends React.Component<{}, State> {
state: State = {};
render() {
const {SelectedRouter} = this.state;
if (!SelectedRouter) return null;
return <><SelectedRouter>asdf</SelectedRouter></>;
}
}
package.json:
{
"name": "temp",
"version": "1.0.0",
"description": "",
"author": "",
"license": "ISC",
"dependencies": {},
"devDependencies": {
"@types/prop-types": "^15.5.6",
"@types/react": "^16.7.11",
"@types/react-dom": "^16.0.11",
"@types/react-router": "^4.0.32",
"@types/react-router-dom": "^4.3.1",
"csstype": "^2.5.7",
"typescript": "^3.3.0-dev.20181129"
}
}
tsconfig.json:
{
"compilerOptions": {
"lib": [ "dom", "es2015" ],
"module": "es2015",
"moduleResolution": "node",
"target": "es6",
"jsx": "react",
},
"include": [ "*.tsx" ]
}
Expected behavior:
Successful compilation.
Actual behavior:
ex.tsx:10:19 - error TS2604: JSX element type 'SelectedRouter' does not have any construct or call signatures.
10 return <><SelectedRouter>asdf</SelectedRouter></>;
~~~~~~~~~~~~~~
Found 1 error.
Notes:
This works with TypeScript 3.1.x, but breaks with 3.2.0+. I am not clear on why certain types work and certain types do not, but I imported the React Router types because they definitely break. I tried creating a test ComponentA
and ComponentB
with a render function that returns null, but using a union of typeof ComponentA | typeof ComponentB
compiles successfully for some reason.