简体   繁体   English

打字稿抱怨使用 React.createClass 时类型“JSX.IntrinsicElements”上不存在属性?

[英]Typescript complains Property does not exist on type 'JSX.IntrinsicElements' when using React.createClass?

I am using typescript to write redux application.我正在使用 typescript 编写 redux 应用程序。

var item = React.createClass({
  render: function() {
    return (<div>hello world</div>)
  }
});

export default class ItemList extends Component<any, any> {
    render() {
        return (<item />)
    }
}

Then typescript complains this:然后打字稿抱怨这个:

Property 'item' does not exist on type 'JSX.IntrinsicElements'.

Your component must start with a capital letter I instead of small letter i otherwise TypeScript would yell.您的组件必须以大写字母I而不是小写字母i开头,否则 TypeScript 会大喊大叫。 Changing item to Item should fix it:item更改为Item应该修复它:

var Item = React.createClass({
  render: function() {
    return (<div>hello world</div>)
  }
});

export default class ItemList extends Component<any, any> {
    render() {
        return (<Item />)
    }
}

You can declare your custom element type like this:您可以像这样声明您的自定义元素类型:

import * as React from 'react'

declare global {
  namespace JSX {
    interface IntrinsicElements {
      item: React.DetailedHTMLProps<React.HTMLAttributes<HTMLElement>, HTMLElement>;
    }
  }
}

That is because your item component's name does not start with a capital letter, causing Typescript to complain.那是因为你的item组件的名字不是以大写字母开头,导致 Typescript 报错。 Replacing item with Item could solve this problem.Item替换item可以解决这个问题。

For anyone else stuck on this.对于其他坚持这一点的人。

The solutions was to解决方案是

rm -rf node_modules
npm install

Typescript complained打字稿抱怨

Property 'div' does not exist on type 'StyledInterface'.

and

Property 'div' does not exist on type 'JSX.IntrinsicElements'.

I think the root cause was vscode (me) accidentally editing type definitions in node_modules .我认为根本原因是 vscode(我)不小心编辑了 node_modules 中的类型定义。

It looked like I had the same problem as in this question, but it was typo :/ But I've decided to share that here since error looked very similar and google led me here.看起来我遇到了与这个问题相同的问题,但它是错字:/但我决定在这里分享它,因为错误看起来非常相似,谷歌把我带到了这里。

I had typo in declaration:我在声明中有错字:

declare global {
    namespace JSX {
        interface IntrinisicElements {
            'about-me': { me: string }
        }
    }
}

Instead of IntrinsicElements I had IntrinisicElements (extra 'i' letter after 'Intrin').而不是IntrinsicElements我有IntrinisicElements ('Intrin' 之后的额外 'i' 字母)。 The compiler didn't complain that because it is definition of interface, so the name can be of our choice.编译器没有抱怨,因为它是接口的定义,所以名称可以由我们选择。

And the error was like that:错误是这样的:

Property 'about-me' does not exist on type 'JSX.IntrinsicElements'.类型“JSX.IntrinsicElements”上不存在属性“about-me”。

Remember: "item" is not valid in TypeScript, you must declare as "Item", UpperCase the first letter!请记住:“item”在 TypeScript 中无效,必须声明为“Item”,首字母大写! It is will be like:它会像:

var Item = React.createClass({
  render: function() {
    return (<div>hello world</div>)
  }
});

Good day!再会!

This could happen if you just renamed a tag by pressing F2 on it in VSCode, which could change the typing of the tag.如果您只是通过在 VSCode 中按F2来重命名标签,则可能会发生这种情况,这可能会更改标签的类型。

When the rename happened, some index.d.ts file should have opened in a new tab.当重命名发生时,一些index.d.ts文件应该在新选项卡中打开。 Check if the tab is still open.检查选项卡是否仍然打开。 The path might be something like .../node_modules/@types/react/index.d.ts .路径可能类似于.../node_modules/@types/react/index.d.ts

If you find the tab, go into it, then press ctrl-z (cmd-z) to undo your change.如果您找到该选项卡,请进入它,然后按 ctrl-z (cmd-z) 撤消您的更改。

If you don't see the tab, you probably have closed it.如果您没有看到该选项卡,您可能已将其关闭。 Try to use your hotkey to reopen recently closed tabs.尝试使用热键重新打开最近关闭的选项卡。 If you don't know the hotkey, use command palette View: Reopen Closed Editor .如果您不知道热键,请使用命令面板View: Reopen Closed Editor

Keep doing it until you find the file, then undo your change.继续这样做,直到找到文件,然后撤消更改。 (VSCode remembers the change history for undo, even after you close the tab) (VSCode 会记住撤消的更改历史记录,即使在您关闭选项卡后也是如此)

我使用 PascalCase 编写组件的名称,然后错误消失

暂无
暂无

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

相关问题 使用本机Web组件时出现打字稿错误“类型&#39;JSX.IntrinsicElements&#39;上不存在属性” - Typescript error “Property does not exist on type 'JSX.IntrinsicElements'” when using native web component TypeScript:“JSX.IntrinsicElements”类型上不存在属性“图标” - TypeScript: Property 'icon' does not exist on type 'JSX.IntrinsicElements' <font>与 Typescript;</font> <font>“JSX.IntrinsicElements”类型上不存在属性“字体”</font> - <font> with Typescript; Property 'font' does not exist on type 'JSX.IntrinsicElements' “JSX.IntrinsicElements”类型上不存在属性“amp-img” - Property 'amp-img' does not exist on type 'JSX.IntrinsicElements' 类型“JSX.IntrinsicElements”上不存在属性“元素” - Property 'element' does not exist on type 'JSX.IntrinsicElements' 类型“JSX.IntrinsicElements”上不存在属性。 TS2339 - Property does not exist on type 'JSX.IntrinsicElements'. TS2339 “JSX.IntrinsicElements”类型上不存在属性“mesh” - Property 'mesh' does not exist on type 'JSX.IntrinsicElements' 带typescript的redux-form生成异常:``类型&#39;JSX.IntrinsicElements&#39;不存在属性&#39;createFunction&#39;。&#39;&#39; - redux-form with typescript generates the exception : ''Property 'createFunction' does not exist on type 'JSX.IntrinsicElements'.'' 打字稿错误:TS2339:类型“JSX.IntrinsicElements”上不存在属性“span” - Typescript Error: TS2339: Property 'span' does not exist on type 'JSX.IntrinsicElements' 如何修复 reactjs 中类型“JSX.IntrinsicElements”上不存在属性“setConfirmDelete” - How to fix the Property 'setConfirmDelete' does not exist on type 'JSX.IntrinsicElements' in reactjs
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM