Sixteen, file type checking

JavaScript file type checking

TS2.3 later versions support the use of --checkJs for .js file type checking and error

  • By // @ ts-nocheck comments to ignore type checks
  • By removing the settings and add a --checkJs // @ ts-check comments to selected check some .js file
  • Use // @ ts-ignore to ignore the Bank of errors

.Js file and compare the differences .ts files on the type of examination, the following points should be noted:

A, type information indicating a type JSDoc

  • js file, and you can type in the file ts as is inferred, may be specified by JSDoc when the type can not be inferred
  • As will TS --noImplicitAny can not be deduced by the compiler of the position error (except in the case of object literal)

Second, from the inferred attribute assignment statement within the class

  • In the js file, the compiler from within the class attribute assignment statements to infer the type of property
  • Type attribute is assigned in the constructor's worth type, unless defined or undefined or null
  • Constructor defined in the property will be considered always present, in the method, the access device is as defined in the attribute is optional
// JSDoc注解修饰的声明会被设置为这个声明的类型
/** @type {number} */
var x;
x = 0;
x = false;
// 属性的推断来自于类内的赋值语句
class PropertyC {
  constructor(){
    /** @type { number | undefined } */
    this.constructorOnly = 0;
    /** @type { number | undefined } */
    this.constructorUnknown = undefined;
  }
  method() {
    this.constructorOnly = false;
    this.constructorUnknown = 'plunkbat';
    this.methodOnly = 'ok';
  }
  method2(){
    this.methodOnly = true;
  }
}

Guess you like

Origin blog.51cto.com/14533658/2436019