简体   繁体   English

是否可以仅对导入的库(Typescript、VSCode)进行类型检查?

[英]Is it possible to type-check only imported libraries (Typescript, VSCode)?

Basic question: Is it possible to type-check only import s?基本问题:是否可以只对import进行类型检查?

I do want to know很想知道

  • TS: property 'id' does not exist on type 'Stripe.customer' (for example) TS: property 'id' does not exist on type 'Stripe.customer' (例如)
  • but not on my own objects, like property 'prop1' does not exist on type '{}'不是在我自己的对象上,比如property 'prop1' does not exist on type '{}'

Background背景

  • I'm working on a Javascript project, and I'd like to use Typescript for added intellisense with imported libraries, while otherwise writing in JS, so I've changed my .js file to .ts during development.我正在开发一个 Javascript 项目,我想使用 Typescript 为导入的库添加智能感知,同时使用 JS 编写,所以我在开发过程中将我的.js文件更改为.ts († see bottom) (†见底部)

  • In the Javascript project, lots of objects are initialized as let x = {} and then properties are later added as x.prop1 = value1 .在 Javascript 项目中,许多对象被初始化为let x = {} ,然后属性稍后被添加为x.prop1 = value1

  • TSLint then warns me, property 'prop1' does not exist on type 'x' .然后 TSLint 警告我, property 'prop1' does not exist on type 'x'

I would like to get this warning only on imports.我只想在进口时收到此警告。

  • For instance, if I import * as stripe from 'stripe' , then it's helpful to get the warning property 'id' doest not exist on type 'customer' .例如,如果我import * as stripe from 'stripe' ,那么获取警告property 'id' doest not exist on type 'customer'会很有帮助。

  • Following this thread, property does not exists on type '{}' , I can achieve this manually on a per-object basis by converting x.prop1 to x['prop1'] .在这个线程之后, 类型 '{}' 上不存在属性,我可以通过将x.prop1转换为x['prop1']在每个对象的基础上手动实现这一点。

  • I've also experimented with inline comments to disable various TSLint rules, but the warnings are coming from the Typescript language features in VSCode, not TSLint.我还尝试过使用内联注释来禁用各种 TSLint 规则,但警告来自 VSCode 中的 Typescript 语言功能,而不是 TSLint。

However, to restate the question more precisely: Is there directive or config option that would allow me to add properties to objects defined in my own code without warning, while still warning/type-checking imported libraries?但是,更准确地重申这个问题:是否有指令或配置选项允许我在没有警告的情况下向我自己的代码中定义的对象添加属性,同时仍然警告/类型检查导入的库?

† If there's a alternative, less janky way to get intellisense on imported libraries in Javascript, I'm all ears! † 如果有另一种更简单的方法来获取 Javascript 中导入的库的智能感知,我洗耳恭听!

Ultimately, I was able to achieve this by最终,我能够通过

  1. keeping my file extension .js instead of .ts (which I was only using becuse I thought it was necessary to enable type checking)保留我的文件扩展名.js而不是.ts (我之所以使用它只是因为我认为有必要启用类型检查)

  2. adding a tsconfig.json file to my project, with the following settings to enable type checking JS:tsconfig.json文件添加到我的项目,使用以下设置启用类型检查 JS:

{
  "compilerOptions": {
    "module": "commonjs",
    "allowJs": true,
    "checkJs": true,
    "strict": false,
    "noImplicitReturns": true,
    "noImplicitAny": false,
    "noUnusedLocals": false,
    "strictPropertyInitialization": false,
    "outDir": "lib",
  },
  "compileOnSave": false,
}

The result was that I could write x.myProp without warning, but importedLibrary.propThatDoesNotExist would throw a Typescript error.结果是我可以在没有警告的情况下编写x.myProp ,但是importedLibrary.propThatDoesNotExist会抛出 Typescript 错误。

And because compileOnSave is false , the compiler didn't actually take any actions, it just issues warnings -- effectively allowing some added TS features in a JS file, which is quite useful.而且因为compileOnSavefalse ,编译器实际上并没有采取任何行动,它只是发出警告——有效地允许在 JS 文件中添加一些TS 特性,这是非常有用的。

I believe this works because it's a Javascript file, and thus the TS compiler expects JS-style coding, but I'm not 100% sure.我相信这是可行的,因为它是一个 Javascript 文件,因此 TS 编译器需要 JS 风格的编码,但我不是 100% 确定。

Finally, a useful reference for the compiler options above: https://www.typescriptlang.org/docs/handbook/compiler-options.html最后,上面的编译器选项的有用参考: https ://www.typescriptlang.org/docs/handbook/compiler-options.html

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

相关问题 如何使用 TypeScript 编译器 API 对使用 `require()` 导入的模块进行类型检查? - How to use the TypeScript Compiler API to type-check modules imported using `require()`? 键入检查TypeScript代码的字符串 - Type-check a string of TypeScript code 我如何在 Typescript 中对这个 object 进行类型检查 - How do I type-check this object in Typescript 如何让TypeScript在React props中对扩展运算符进行类型检查? - How can I get TypeScript to type-check the spread operator in React props? 有没有办法对另一个 function 的 arguments 中的 Typescript 中的函数参数进行类型检查? - Is there a way to type-check just a function's parameters in Typescript in the arguments of another function? 为什么类型保护无法通过类型检查 - Why is a type guard failing type-check 是否可以在 VSCode(或其他地方)中显示打字稿类型/接口的完整计算类型 - Is it possible to display the full computed type of a typescript type/interface in VSCode (or elsewhere) 是否可以在 TypeScript 中的类型定义中进行类型检查 - Is it possible to have a type check in type definition in the TypeScript 记录中动态键的 TS 类型检查错误 - TS type-check error on dynamic key in record 类型检查功能,用于在预定义的接口上设置值 - type-check on function that sets values on a pre-defined interface
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM