TypeScript React function-returning-component regression

muk1a3rh  于 4个月前  发布在  TypeScript
关注(0)|答案(2)|浏览(39)

TypeScript版本: 3.2.0-dev.20181103
代码

import React = require("react");
declare function F(): C;
class C extends React.Component<{ title?: string }> {}
<F title="My Title" />

预期行为:

没有错误,就像ts3.1一样。

实际行为:

src/a.tsx:4:2 - error TS2322: Type '{ title: string; }' is not assignable to type 'IntrinsicAttributes'.
  Property 'title' does not exist on type 'IntrinsicAttributes'.

4 <F title="My Title" />
   ~

Found 1 error.

在DefinitelyTyped上发现了react-helmet/v4react-flag-icon-cssreact-flag-icon-css的变化更像这样:

import React = require("react");
declare class C extends React.PureComponent<{}> {}
declare const F: (props: {}) => C;
<C />; // OK
<F />; // Error
bvjxkvbb

bvjxkvbb1#

这...不应该起作用。它永远不应该被类型检查 - 一个返回组件(或组件示例)的函数不是一个jsx元素。如今,React甚至在运行时都有一个警告:

Functions are not valid as a React child. This may happen if you return a Component instead of <Component /> from render. Or maybe you meant to call this function rather than return it.

这些测试直接包含无效的React代码,本应该从未正确地被类型检查过,也不会在运行时起作用。

whlutmcx

whlutmcx2#

我们需要解决这个问题来避免无效的React代码,对吗?

$x^{#26182}$

相关问题