visual studio code + react-native + typescript: jsx intellisense 不工作

visual studio code + react-native + typescript: jsx intellisense not working

这很奇怪。我正在使用 visual studio 代码,设置了带有 typescript 的 react-native(react-native + nativebase + typescript)。虽然智能感知和类型检查通常有效,但我在使用 jsx 智能感知 时遇到了一些麻烦,代码建议不会出现在任何 jsx 属性中。

我的意思是,通常当你在 jsx 元素中粘贴 ctrl+space 时,vs 代码会显示可用的道具。

这行不通。在上面的示例中,提供的建议不是此 jsx 元素 <Content> 的可用属性。结果是 props 列表中的错误不会被编译器发现。例如这段代码:

即使 foo 属性不存在于 Content 组件的定义中,

也不会被标记为错误。它没有在任何地方定义。这是它第一次出现在代码中。然而编译器不会将其标记为错误。

Typescript 应该会造成 javascript typed and pick compile time 错误。

这是我的 tsconfig.json:

{
    "compilerOptions": {
        "target": "es6",
        "allowJs": true,
        "jsx": "react",        
        "rootDir": "modules",
        "sourceMap": true,
        "noImplicitAny": false,     
    },    
    "filesGlob": [        
        "modules/**/*.ts",
        "modules/**/*.tsx",
        "typings/**/*.d.ts"
    ],    
    "exclude": [
        "node_modules"        
    ]
}

打字稿也在visual studio代码中正确设置,如任务栏所示:

可以看到设置了Typescript with react。在我的 tsx 文件中也正确引用了类型:

/// <reference path="../typings/index.d.ts" />

import * as React from 'react';
import { View, Text } from 'react-native';

那么我怎样才能让打字稿通过 ctrl + space 在 jsx 元素上正确地建议道具,更严重的是,为什么当我使用给定组件未定义的道具时打字稿没有检测到错误?是我没有得到的打字还是 tsconfig.json 中有设置?

感谢您的宝贵帮助。

对于任何有类似问题的人,我通过使用 npm @types (例如 npm install @types/react )在我的项目中引入类型来解决我自己的问题而不是打字或 tds。