【发布时间】:2017-05-08 16:38:23
【问题描述】:
这很奇怪。我正在使用 Visual Studio 代码,并设置了带有 typescript 的 react-native(react-native + nativebase + typescript)。虽然智能感知和类型检查通常有效,但我在使用 jsx 智能感知 时遇到了一些问题,任何 jsx 道具都不会出现代码建议。
我的意思是,通常当您在 jsx 元素中使用 ctrl+space 时,vs 代码会显示可用的道具。
这不起作用。在上面的例子中,提供的建议不是这个 jsx 元素<Content> 的可用道具。结果是道具列表中的错误不会被编译器拾取。例如这段代码:
即使 foo 不存在于 Content 组件的定义中,也不会被标记为错误。它没有在哪里定义。这是它第一次出现在代码中。然而编译器并没有将其标记为错误。
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 代码中也正确设置了打字稿,如任务栏所示:
你可以看到带有 react 的 Typescript 已经设置好了。在我的 tsx 文件中也正确引用了类型:
/// <reference path="../typings/index.d.ts" />
import * as React from 'react';
import { View, Text } from 'react-native';
那么,我怎样才能让 typescript 正确建议带有 ctrl + 空格的 jsx 元素上的 props,更严重的是,为什么当我使用未为给定组件定义的 props 时,typescript 没有检测到错误?是我没有输入的东西还是 tsconfig.json 中有设置?
感谢您的宝贵帮助。
【问题讨论】:
标签: typescript react-native visual-studio-code intellisense react-jsx