本小说就对她小规模试制牛刀一下,本小说就对他小规模试制牛刀一下

新近在念书typescript的时候,发现ts居然可以对原生js做强行检查查测试。发现从v二.三就起来向来了。那让本身感慨他的扭转,又让自家本人感觉学习的迟缓。本小说就对她小规模试制牛刀一下。

近年来在上学typescript的时候,发现ts居然能够对原生js做强行检查查实验。发现从v二.叁就起来向来了。这让自家感慨他的浮动,又让自己要青睐觉学习的缓慢。本小说就对他小规模试制牛刀一下。

1、他是的选拔与协理

透过阅读官网的求证,驾驭到他促成认证是因此jsdoc的笺注结构实现。然后在.js文件底部参与如投注释标签即可:

标记 说明
// @ts-nocheck 标记此文件不做类型检测
// @ts-check 标记此文件做类型检测,但没有用–checkJS参数时
// @ts-ignore 标记后面一段不做类型检测(可以说是后面一行)

一、他是的接纳与帮助

透过阅读官网的求证,掌握到他贯彻认证是因此jsdoc的笺注结构达成。然后在.js文件底部到场如投注释标签即可:

标记 说明
// @ts-nocheck 标记此文件不做类型检测
// @ts-check 标记此文件做类型检测,但没有用–checkJS参数时
// @ts-ignore 标记后面一段不做类型检测(可以说是后面一行)

2、示例展示

简易示例代码如下:

// @ts-check

/**
 * @type {number}
 */
var x;

x="12";

成效图如下:

图片 1

是或不是很神奇,编写翻译环境能识别出类型的反差。其余测试截图如下:

图片 2

2、示例展现

简短示例代码如下:

// @ts-check

/**
 * @type {number}
 */
var x;

x="12";

职能图如下:

图片 3

是还是不是很神奇,编写翻译环境能分辨出类型的反差。其余测试截图如下:

图片 4

三、疑问

  1. 服从官网的表明,对object对象也可以形成检验,但测试貌似不得以,还望各位扶助解疑(官网原话如下):

三、疑问

  1. 依照官网的表明,对object对象也足以形成检查测试,但测试貌似不可以,还望各位支持解疑(官网原话如下):

Object literals are open-ended

By default object literals in variable declarations provide the type of
a declaration. No new members can be added that were not specified in
the original initialization. This rule is relaxed in a .js file;
object literals have an open-ended type, allowing adding and looking up
properties that were not defined originally. For instance:

var obj = { a: 1 };
obj.b = 2;  // Allowed

Object literals get a default index signature [x:string]: any that
allows them to be treated as open maps instead of closed objects.

Similar to other special JS checking behaviors, this behavior can be
changed by specifying a JSDoc type for the variable. For example:

/** @type  */
var obj = { a: 1 };
obj.b = 2;  // Error, type {a: number} does not have property b

Object literals are open-ended

By default object literals in variable declarations provide the type of
a declaration. No new members can be added that were not specified in
the original initialization. This rule is relaxed in a .js file;
object literals have an open-ended type, allowing adding and looking up
properties that were not defined originally. For instance:

var obj = { a: 1 };
obj.b = 2;  // Allowed

Object literals get a default index signature [x:string]: any that
allows them to be treated as open maps instead of closed objects.

Similar to other special JS checking behaviors, this behavior can be
changed by specifying a JSDoc type for the variable. For example:

/** @type  */
var obj = { a: 1 };
obj.b = 2;  // Error, type {a: number} does not have property b

相关文章