简体   繁体   English

如何在不键入JS子组件的情况下将未类型化的ES6 react组件导入Typescript组件?

[英]How do you import untyped ES6 react components into a Typescript component without typing the JS child components?

If I have an ES6 component like so: 如果我有这样的ES6组件:

 component OldThing extends React.Component { constructor(props) { super(props); } render() { return <div>{this.props.someProp}</div>; } } 

an ES6 container like so: 像这样的ES6容器:

 const OldThingContainer = connect(mapStateToProps, mapDispatchToProps)(OldThing); 

and a new typescript component like so: 和一个新的打字稿组件,如下所示:

 component NewThing extends React.Component<Props, State> { render() { return <OldThingContainer someProp={someValue} /> } } 

I currently get an IntrinsicAttributes error about someProp not existing on OldThing. 我目前收到有关OldThing上不存在的someProp的IntrinsicAttributes错误。 How do I get rid of that error without adding types/typedefs to/for OldThing and/or OldThingContainer? 如何在不为OldThing和/或OldThingContainer添加类型/ typedef的情况下摆脱该错误? I tried the @augments solution on the OldThingContainer where it's defined but that doesn't seem to help. 我在定义它的OldThingContainer上尝试了@augments解决方案,但这似乎无济于事。 Neither did adding the comment on the component itself. 也没有在组件本身上添加注释。

The reason I want to be able to import JS without types is that we are adding typescript to a very large, multiple years old code base and it's currently difficult to get people to write typescript components at all, let alone if they also have to type out every existing component their component wants to import. 我希望能够不带类型地导入JS的原因是,我们正在将打字稿添加到一个很大的,已有多年历史的代码库中,并且目前很难使人们完全编写打字稿组件,更不用说他们是否也必须打字删除其组件要导入的每个现有组件。

Is there an existing elegant solution to this problem or am I going to have to go through ahead of everyone and manually type every existing JS component (hundreds) in some typedef file to get rid of/prevent these errors? 是否存在解决此问题的优雅解决方案,还是我将不得不先经过每个人并在某个typedef文件中手动键入每个现有的JS组件(数百个)以消除/防止这些错误?

I tried your example and it seemed some issue with the type connect inferred for your component. 我尝试了您的示例,并且为您的组件推断出的connect类型似乎有些问题。

One quick'n'dirty fix would be to annotate your JS component with: 一种快速的解决方法是使用以下方法注释您的JS组件:

/**
 * @type {React.ComponentType<any>}
 */
const OldThingContainer = connect(mapStateToProps, mapDispatchToProps)(
  OldThing
);

One more elegant fix (but maybe not possible) would be trying to dive into the react-redux typings. 一种更优雅的解决方案(但可能无法实现)将试图深入到react-redux The connect function requires you to supply some type arguments for it to properly work. connect函数要求您提供一些类型参数以使其正常工作。 When not supplied, those arguments are given {} , not any (because any can silently eat your types). 如果未提供,这些参数将被赋予{} ,而不是any (因为any参数都可以悄悄地吞噬您的类型)。

Comment: From the few I've worked with react+redux+typescript, lib typedefs are one of the biggest pain points. 评论:从我使用react + redux + typescript进行的工作中,lib typedef是最大的痛点之一。 React typedefs can get really complicated. React typedef会变得非常复杂。 (flow just will hang up your editor with "waiting for flow server", which is even worse). (流只会使您的编辑器挂起“等待流服务器”,这甚至更糟)。 As TS+Redux gets more popular and more support is added, this may get a lot better in a few months... 随着TS + Redux变得越来越流行并且添加了更多支持,这在几个月内可能会变得更好...

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM