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

时间:2016-12-23 00:19:36

标签: typescript react-native visual-studio-code intellisense react-jsx

这很奇怪。我正在使用visual studio代码,已经使用typescript设置了react-native(react-native + nativebase + typescript)。虽然intellisense和类型检查一般工作,我遇到一些问题 jsx intellisense ,代码建议不会出现任何jsx道具。

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

enter image description here

这不起作用。在上面的示例中,提供的建议不是此jsx元素<Content>的可用道具。结果是编译器没有拾取道具列表中的错误。例如这段代码:

enter image description here

即使内容组件的定义中不存在道具 foo

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

Typescript应该使javascript类型化并选择编译时错误。

这是我的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,如任务栏所示:

enter image description here

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

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

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

那么我如何才能使用ctrl + space来正确地在jsx元素上建议道具?更严重的是当我使用未定义给定组件的道具时,为什么打字稿没有检测到错误?是不是有些东西我不知道或者tsconfig.json中有没有设置?

感谢您的宝贵帮助。

1 个答案:

答案 0 :(得分:2)

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