-
Notifications
You must be signed in to change notification settings - Fork 4.1k
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
TypeScript: can't import components individually #1182
Comments
I've investigated this issue, problem is quite simple. When you write this in TS, it's correct because typings doesn't have default export. import { Icon } from 'semantic-ui-react/dist/commonjs/elements/Icon';
import { IconGroup } from 'semantic-ui-react/dist/commonjs/elements/Icon'; However, when it's transpiled in JS, it's incorrect, import Icon from 'semantic-ui-react/dist/commonjs/elements/Icon';
import IconGroup from 'semantic-ui-react/dist/commonjs/elements/Icon/IconGroup'; But, it will not work with TS, because typings says another 🤔 So, we need update typings as: -export const Icon: IconComponent;
+export default IconComponent;
// ...
-export const IconGroup: React.StatelessComponent<IconGroupProps>;
+const IconGroup: React.StatelessComponent<IconGroupProps>; But, I'm not sure that this will work after these updates: import { Icon, IconGroup } from 'semantic-ui-react' |
Ah now I can see the problem, thanks @layershifter. It seems that the // src/elements/Icon/Icon.js
...
export default Icon and // src/elements/Icon/index.js
export default from './Icon' but // src/elements/Icon/index.d.ts
...
export const Icon: IconComponent;
...
export const IconGroup: React.StatelessComponent<IconGroupProps>; There are two issues here:
There are a couple of ways to solve this:
// src/elements/Icon/index.d.ts
...
export default IconComponent;
...
// export const IconGroup: React.StatelessComponent<IconGroupProps>;
// src/elements/Icon/index.js
export { default as Icon} from './Icon'
export { default as IconGroup } from './IconGroup' I'd vote for option 1., since option 2. isn't backwards compatible. EDIT: just realized 1. is exactly what @layershifter proposed, sorry for not reading more thoroughly |
After some testing, in Icon's case for example, the typings would need to be changed as follows: // src/elements/Icon/index.d.ts
declare const Icon: IconComponent;
export default Icon; and // index.d.ts
...
export { default as Icon } from './dist/commonjs/elements/Icon';
... Note the addition of With this both of these work: import { Icon } from 'semantic-ui-react'; import Icon from 'semantic-ui-react/dist/commonjs/elements/Icon'; |
@dennari your final solution proposed above #1182 (comment) seems correct to me. In this case, there are separate files for Icon and IconGroup, true? |
Would you like to add this work to your PR for testing typings, #1181? |
@levithomason , the most complete solution would involve having a separate typings file for Yes, I'll modify #1181 to reflect this issue once we have agreed exactly what we want to export. |
This is true, although, we export all subcomponents at the top level for those who do not want to (or cannot) use the static property notation, see #554. I'm fine with it either way in this case. Thanks for the update as well. |
Here are some thoughts that I have. ImportsWe need to change typings structure to correspond JS-files:
It is reasonable, simple and will solve all problems with imports. TestingI think with need to add a new conformance test
In last test, we will need to use the AST tree formed by typescript compiler. Test's code can be written in JS, so we can use karma to run it. @levithomason Other ideas? |
This is more inline with the direction I was thinking. The only thought I would add here is that we should consider generating typings completely. It may take quite a bit of work, however, we are investing a large amount of work into constant maintenance as well. I'm beginning to wonder if the work for long term TS maintenance outweighs the work required to create a successful TS interface generator. |
@levithomason It's interesting idea, but I think it will be difficult to implement 🔨 However, you and I agree with the changes of typings structure.
I will be glad to see a PR to change the typings structure. |
#1395 was merged. |
Released in |
When I try to import an individual component, for example
I get a runtime error:
For some reason
Icon
isundefined
. I added a PR which includes a test for this scenario: #1181.The text was updated successfully, but these errors were encountered: