-
Notifications
You must be signed in to change notification settings - Fork 12.6k
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
TS files cannot use JSX components declared as a classes in JS/JSX file #14558
Comments
Can you give #14396 a try and see if it fixes the issue? |
This is killing me trying to migrate a large project to Typescript. I've got a hundred plain ol React components and I'd love to be able to do 1 at a time, but once converted to .tsx, they can't import any other .js files without this error. |
This is addressed by #14907. @timothyallan please give |
This is not solved with |
I see it working on latest. if you are seeing issues please file a new issue: c:\test\14558>type tsconfig.json
{
"compilerOptions": {
"module": "es2015",
"target": "esnext",
"jsx": "react-native",
"strictNullChecks": true,
"allowSyntheticDefaultImports": true,
"allowJs": true,
"checkJs": true,
"lib": [
"es5",
"es6",
"es7",
"es2017",
"dom"
],
"outDir": "dist"
},
"include": [
"**/*.ts",
"**/*.tsx",
"**/*.js"
],
"exclude": [
"dist",
"node_modules"
]
}
c:\test\14558>type index.tsx
import React from 'react';
import Hello from './hello';
class A extends React.Component<void, void> {
render() {
return (
<div>
<Hello>
<div></div>
</Hello>
</div>
);
}
}
c:\test\14558>type hello.js
// @ts-check
import React from 'react';
export default class Hello extends React.Component {
render() {
return <div>Hello</div>;
}
}
c:\test\14558>tsc --v
Version 2.3.2
c:\test\14558>tsc --p tsconfig.json
c:\test\14558>echo %ERRORLEVEL%
0 |
does it work if you rename hello.js to hello.jsx ? |
c:\test\14558>move hello.js hello.jsx
1 file(s) moved.
c:\test\14558>
tsc
c:\test\14558>echo %ERRORLEVEL%
0 |
Did you try adding properties to Here is the compiler output:
It can be worked around with React.createElement(Hello, {baz: "world"} as any) or by creating temporary variable with properties but this is quite ugly workaround. Can this be done better? |
https://github.com/gagarski/tsx-jsx - complete minimalistic example |
Can we please reopen it? It doesn't work if component have any props inside and it's a show stopper for React projects which are migrating to TypeScript |
@mhegazy this issue has not been resolved. Is there anyone looking at this? |
Agree, it's not resolved. I too am facing the same issue and would like a resolution. |
+1 I have the same problem guys |
The original issue was fixed. but then a change was made to the react.d.ts to make so the default props is this is the same issue reported in #18134.. the fix is to change the react declaration file to be |
I tried to monkey-patch Here is patched
But I still get a compilation error for this example:
However, when I am using |
you are right. we need to remove the default or make it |
@weswigham can you take a look here. unfortunately we can not remove the defaults from react definition file as that would be a big breaking change, but what can we do not make sure this scenario still works. |
Yeah, removing the default would broke a lot of code and |
sadly also suffering from this issue -- is a huge deal when trying to migrate incrementally to typescript. |
This worked for me |
I'm bumping into this as well. For now, thinking the only sane way to migrate to Typescript is to start with components that don't import any other |
So we know the root cause of this issue - While we work on it, a workaround in the meantime is editing the import React, { Component } from "react";
/**
* @augments {Component<{location: string}, *>}
*/
class MyComponent extends Component {
render() {
return <h1>Hello World from {this.props.location}</h1>
}
}
const x = <MyComponent location="Redmond" /> |
Helps for me //this is tsx file
import React, { Component } from "react";
import SomeComponent from './SomeComponent.js';
/* tslint:disable */
const JsSomeComponent: any = SomeComponent; //todo delete me after refactoring to TS
/* tslint:enable */
class MyTSXComponent extends Component {
public render() {
return <JsSomeComponent/>;
}
} |
We have a fix on the compiler side in #19977 to accommodate the react changes. Can you give tomorrow's |
Tempted to try new fix. For the time being or for everyone stuck on old versions in future:
|
@mhegazy this fix does not work if |
Well.. it is an implicit any... if you want the strict no- |
@mhegazy so in |
@mhegazy This makes it very difficult to incrementally adopt TS in a large codebase because we have hundreds/thousands of JS components that will not be converted for the foreseeable future. |
but it is an implicit |
so i miss understood the @vkrol's comment. I thought he meant getting an error under |
#20232 should fix that. |
Just ran into this as well, just so I'm understanding, with #20232 this fix should work for an incrementally converted codebase when using a non TS React component inside of a TSX component with noImplicitAny enabled |
@mhegazy @weswigham it is works in the latest nightly build. Thanks! |
I ran into this as well.. while using |
@danantal Running into this as well. Not on TS |
Det er dratt inn fra nav-frontend-moduler. Per nå så opplever jeg litt krøll med komponenten, sannsynligvis fordi den ikke er skrevet i React. InteliJ rapporterer at 'EkspanderbartpanelBase does not have any construct or call signature'. Det funker fint å kompilere og kjøre, men skriptet feiler ved hot-reload av visittkort-komponenten. Det kan være relatert til denne: microsoft/TypeScript#14558. Denne har blitt merget inn i master, men er ikke ute i en release enda. Det eksisterer også en PR i nav-frontend på å skrive om panelet til typecript som forhåpentligvis vil løse problemet: navikt/aksel#233
This works for me:
|
CC @donataswix
It appears that using a React component written as a class in a
.js
file causes some issues.Start a project with the following files:
tsconfig.json
index.tsx
hello.js
Expected: No problems
Actual: Error on
<Hello />
: JSX element type 'Hello' does not have any construct or call signatures.Note that if you add type arguments to
Component
inhello.js
, this will actually fix the problem (while creating new ones).Potentially related is #13609.
The text was updated successfully, but these errors were encountered: