-
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 #15596
Comments
|
Ignore the output. The error is different. Let me try to repro with a copy of the real files. |
ok. so here is the weird thing that is happening. the bug does not happen in the test directory However in our repo, in which I replace the code such that index.tsx is pointing just to hello.jsx(with the same paths) the error happens. Now two things are different in that repo
Somehow (2) might be trickling a bug with the compiler but isolated two class tests are not able to repro this... That is the insight I have so far. |
@r-tock i will need a self contained repro to be able to debug more and identify the issue. can you share more context for this issue. |
I know...I am trying my best to isolate. The only difference between the self-contained version and my version is the other files in the repo, which should not be read or resolved by the compiler. I am slowly deleting a file one by one to see what is causing the issue |
thanks! |
Ok I now have a repro. This has to do with the way we were referencing files outside the folder and that works for our babel based jsx code, typescript resolver maybe doing something weird here. robinanil 14:55:37 :~/work/bug$ ls
main shared
robinanil 14:55:46 :~/work/bug$ ls -lh main/
total 40
drwxr-xr-x 4 robinanil staff 136B May 7 14:51 dist
drwxr-xr-x 28 robinanil staff 952B May 7 14:50 node_modules
-rw-r--r-- 1 robinanil staff 618B May 4 22:07 package.json
lrwxr-xr-x 1 robinanil staff 10B May 7 14:55 shared -> ../shared/
drwxr-xr-x 3 robinanil staff 102B May 7 14:53 src
-rw-r--r-- 1 robinanil staff 573B May 7 14:55 tsconfig.json
-rw-r--r-- 1 robinanil staff 6.7K May 7 14:50 yarn.lock
robinanil 14:55:54 :~/work/bug$ tree main/src/
main/src/
└── js
└── index.tsx
1 directory, 1 file
robinanil 14:56:01 :~/work/bug$ tree shared/
shared/
└── src
└── js
├── BaseComponent.jsx
└── hello.jsx
2 directories, 2 files
robinanil 14:56:04 :~/work/bug$ cat main/src/js/index.tsx
import * as React from 'react';
import Hello from 'shared/hello';
class A extends React.Component<any, any> {
render() {
return (
<div>
<Hello skip={true} />
</div>
);
}
}
robinanil 14:56:11 :~/work/bug$ cat shared/src/js/
BaseComponent.jsx hello.jsx
robinanil 14:56:11 :~/work/bug$ cat shared/src/js/BaseComponent.jsx
import React from 'react';
/**
* The base component that autobinds all the methods to this.
*/
class BaseComponent extends React.Component {
constructor(props) {
super(props);
Object.getOwnPropertyNames(Object.getPrototypeOf(this)).forEach((method) => {
if (typeof this[method] !== 'function') {
return;
}
this[method] = this[method].bind(this);
});
}
}
export default BaseComponent;
robinanil 14:56:19 :~/work/bug$ cat shared/src/js/hello.jsx
import React from 'react';
import BaseComponent from './BaseComponent'
class Hello extends BaseComponent {
render() {
if (this.props.skip) {
return null;
}
return <div>Hello</div>;
}
}
export default Hello;
robinanil 14:56:22 :~/work/bug$ cat main/tsconfig.json
{
"compilerOptions": {
"module": "es2015",
"target": "esnext",
"jsx": "react-native",
"strictNullChecks": true,
"experimentalDecorators": true,
"allowSyntheticDefaultImports": true,
"moduleResolution": "node",
"allowJs": true,
"checkJs": true,
"outDir": "dist",
"lib": [
"es5",
"es6",
"es7",
"es2017",
"dom"
],
"baseUrl": ".",
"rootDir": "..",
"paths": {
"shared/*": ["../shared/src/js/*"]
}
},
"exclude": [
"dist"
],
"files": [
"src/js/index.tsx"
]
} $ tsc -p tsconfig.json
src/js/index.tsx(8,9): error TS2605: JSX element type 'Hello' is not a constructor function for JSX elements.
Property 'setState' is missing in type 'Hello'.
src/js/index.tsx(8,9): error TS2607: JSX element class does not support attributes because it does not have a 'props' property.
../shared/src/js/BaseComponent.jsx(1,19): error TS2307: Cannot find module 'react'.
../shared/src/js/hello.jsx(1,19): error TS2307: Cannot find module 'react'.
../shared/src/js/hello.jsx(7,14): error TS2339: Property 'props' does not exist on type 'Hello'. If I change the path to use shared directory symlink $ ln -s ../shared shared - "shared/*": ["../shared/src/js/*"]
+ "shared/*": ["./shared/src/js/*"] $ tsc -p tsconfig.json
$ echo $?
0 |
Still no luck reproducing this... what is your OS? |
MacOS Sierra 10.12.14. Using nvm (for node installation) |
i will need to find a mac to test this on.. |
I am on Node 7.9 |
@mhegazy .... I can take a look at this on my mac |
thanks! |
I see. thanks. this makes sense. when resolving the module import to the outer file, its import to using |
using symlink for shared is actually a work around... We use a mono repo and does not follow the npm+git packaging model. For us, various app directories are siblings of shared or libraries. So we cannot afford to symlink all the time this was just an illustration, I am at the moment converting our large codebase to typescript. How can I make the resolver look in main\node_modules for types? |
depending on what you are looking for, for "paths": {
"shared/*": ["../shared/src/js/*"],
"*": ["node_modules/*", "node_modules/@types/*"]
} should do the trick. |
Thanks this helps a lot. Closing the issue |
This is on a macbook. That is the only difference I can see
Opening a new bug offshoot of #14558
@mhegazy
The text was updated successfully, but these errors were encountered: