美文网首页让前端飞
TypeScript——JavaScript文件类型检查(五)

TypeScript——JavaScript文件类型检查(五)

作者: 2o壹9 | 来源:发表于2019-12-31 09:34 被阅读0次

更多示例

var someObj = {

  /**

  * @param {string} param1 - Docs on property assignments work

  */

  x: function(param1){}

};

/**

* As do docs on variable assignments

* @return {Window}

*/

let someFunc = function(){};

/**

* And class methods

* @param {string} greeting The greeting to use

*/

Foo.prototype.sayHi = (greeting) => console.log("Hi!");

/**

* And arrow functions expressions

* @param {number} x - A multiplier

*/

let myArrow = x => x * x;

/**

* Which means it works for stateless function components in JSX too

* @param {{a: string, b: number}} test - Some param

*/

var sfc = (test) => <div>{test.a.charAt(0)}</div>;

/**

* A parameter can be a class constructor, using Closure syntax.

*

* @param {{new(...args: any[]): object}} C - The class to register

*/

function registerClass(C) {}

/**

* @param {...string} p1 - A 'rest' arg (array) of strings. (treated as 'any')

*/

function fn10(p1){}

/**

* @param {...string} p1 - A 'rest' arg (array) of strings. (treated as 'any')

*/

function fn9(p1) {

  return p1.join();

}

已知不支持的模式

在值空间中将对象视为类型是不可以的,除非对象创建了类型,如构造函数。

function aNormalFunction() {

}

/**

* @type {aNormalFunction}

*/

var wrong;

/**

* Use 'typeof' instead:

* @type {typeof aNormalFunction}

*/

var right;

对象字面量属性上的=后缀不能指定这个属性是可选的:

/**

* @type {{ a: string, b: number= }}

*/

var wrong;

/**

* Use postfix question on the property name instead:

* @type {{ a: string, b?: number }}

*/

var right;

Nullable类型只在启用了strictNullChecks检查时才启作用:

/**

* @type {?number}

* With strictNullChecks: true -- number | null

* With strictNullChecks: off  -- number

*/

var nullable;

Non-nullable类型没有意义,以其原类型对待:

/**

* @type {!number}

* Just has type number

*/

var normal;

不同于JSDoc类型系统,TypeScript只允许将类型标记为包不包含null。 没有明确的Non-nullable -- 如果启用了strictNullChecks,那么number是非null的。 如果没有启用,那么number是可以为null的。

相关文章

网友评论

    本文标题:TypeScript——JavaScript文件类型检查(五)

    本文链接:https://www.haomeiwen.com/subject/jmfboctx.html