typescript 如何禁用所有打字稿类型检查?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/54506744/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How can I disable all typescript type checking?
提问by TIMEX
I would like to use TypeScript in the future, but for right now, I have chosen to install TypeScript in Create React App. (Later, I will go back and add types)
我想在未来使用 TypeScript ,但现在,我选择在 Create React App 中安装 TypeScript。(后面我会回去加类型)
Therefore, I would like to disable all type checks.
因此,我想禁用所有类型检查。
Right now, when I do something like this:
现在,当我做这样的事情时:
<PlaceSearchBar
placeSearchChanged={this.placeSearchChanged}
/>
class PlaceSearchBar extends React.Component {
...
}
I get an error:
我收到一个错误:
Type error: Type '{ placeSearchChanged: (place: any) => void; }' is not assignable to type 'IntrinsicAttributes & IntrinsicClassAttributes<PlaceSearchBar> & Readonly<{ children?: ReactNode; }> & Readonly<{}>'.
Property 'placeSearchChanged' does not exist on type 'IntrinsicAttributes & IntrinsicClassAttributes<PlaceSearchBar> & Readonly<{ children?: ReactNode; }> & Readonly<{}>'. TS2322
Apparently I need to declare types in React.Component<placeSearchChanged:function>
or something of that sort.
显然我需要声明类型React.Component<placeSearchChanged:function>
或类似的东西。
I think it's annoying, and I would like to disable all checks in my tsconfig.json
.
我觉得这很烦人,我想禁用我的tsconfig.json
.
How can I disable all checks (but still keep TypeScript installed, just for future-proof)?
如何禁用所有检查(但仍保持安装 TypeScript,只是为了面向未来)?
This is my current tsconfig.json:
这是我当前的 tsconfig.json:
{
"compilerOptions": {
"target": "es6",
"lib": [
"dom",
"dom.iterable",
"esnext",
],
"allowJs": true,
"skipLibCheck": true,
"esModuleInterop": true,
"allowSyntheticDefaultImports": true,
"strict": false,
"forceConsistentCasingInFileNames": true,
"module": "esnext",
"moduleResolution": "node",
"resolveJsonModule": true,
"isolatedModules": true,
"noEmit": true,
"jsx": "preserve",
"noImplicitAny": false,
},
"include": [
"src"
]
}
回答by Karol Majewski
Add this to your tsconfig.json
:
将此添加到您的tsconfig.json
:
{
"compilerOptions": {
...
"checkJs": false
...
}
}
and stick to .js
/.jsx
files for now. Use the .ts
/.tsx
extension only when you're ready to use types.
现在坚持使用.js
/.jsx
文件。仅当您准备好使用类型时才使用.ts
/.tsx
扩展名。
If you would rather suppress the errors on a per-line basis, you can use a // @ts-ignore
comment.
如果您希望逐行抑制错误,您可以使用// @ts-ignore
注释。
回答by nologin
Typescript without types is Javascript.
Start your project without Typescript and convert it when you are ready to do so.
Beginning with <any>
is not a good practice and makes no sense in my opinion.
没有类型的打字稿是 Javascript。在没有 Typescript 的情况下启动您的项目,并在您准备好时进行转换。开始<any>
不是一个好习惯,在我看来没有任何意义。
回答by Dave
I agree with nologin, there is no point doing that, however if you really want to there are a few ways that I can think of, here is a couple:
我同意 nologin,这样做没有意义,但是如果你真的想要,我可以想到几种方法,这里有一些:
Disable by file
按文件禁用
add this comment at the top of the file /* tslint:disable */
在文件顶部添加此注释 /* tslint:disable */
Exclude your src folder
排除您的 src 文件夹
Exclude your code folders from tslint.json (might need to do it on tsconfig.json too
从 tslint.json 中排除您的代码文件夹(也可能需要在 tsconfig.json 上执行此操作
{
// some linting options
linterOptions: {
exclude: ['src/**','components/**'],
}
}
Empty tslint.json and tsconfig
清空 tslint.json 和 tsconfig
just replace your tslint.json and tsconfig.json files with an empty object
只需用空对象替换 tslint.json 和 tsconfig.json 文件
回答by Harshit Juneja
Typescript's USP is type checking at compile time. It ultimately compiles to javascript. Start without typescript. You can always include it in your project when you want to with some refactoring.
Typescript 的 USP 是在编译时进行类型检查。它最终编译为 javascript。从没有打字稿开始。当您想要进行一些重构时,您始终可以将它包含在您的项目中。
https://www.typescriptlang.org/docs/handbook/migrating-from-javascript.html
https://www.typescriptlang.org/docs/handbook/migrating-from-javascript.html
回答by user1191175
you can let the build happen even in presence of typescript errors https://create-react-app.dev/docs/advanced-configuration/#! ctr+f TSC_COMPILE_ON_ERROR
即使存在打字稿错误https://create-react-app.dev/docs/advanced-configuration/#,您也可以让构建发生 !ctr+f TSC_COMPILE_ON_ERROR