随笔分类 -  TS

摘要:interface Bird{ fly: boolean, sing: ()=>{} } interface Dog{ fly: boolean, bark: ()=>{} } /** * animal: Bird | Dog这就是一个联合类型 * animal 既可以是 Bird 又可以是 Dog 阅读全文
posted @ 2020-06-17 06:48 wzndkj 阅读(314) 评论(0) 推荐(0)
摘要:tsc --init 会生成一个 tsconfig.json 的配置文件,tsc 没法用需要全局安装 typescript。这个文件是对 ts 的编译配置文件。我们新建一个文件夹,生成 tsconfig.json,将 "removeComments": true, 这个配置放开,在编译的时候,把注释 阅读全文
posted @ 2020-06-17 06:24 wzndkj 阅读(4937) 评论(0) 推荐(0)
摘要:1、新建文件夹 demo 2、npm init -y 生成 package.json 文件 3、tsc --init 生成 tsconfig.json,可以对 ts 的默认配置做修改 4、npm install -D(--save-dev) ts-node npm install -D typesc 阅读全文
posted @ 2020-06-07 09:18 wzndkj 阅读(562) 评论(0) 推荐(0)
摘要:类属性只能读不能写的两种方式 class Person { constructor(public name:string){} } const person = new Person('zina'); person.name = 'hello'; // 这里可以修改 name console.log 阅读全文
posted @ 2020-06-05 06:53 wzndkj 阅读(572) 评论(0) 推荐(0)
摘要:class Person { constructor(private _name: string){} // 对于私有的属性进行处理后再暴露出去,比如加密,确保安全 get name(){ return this._name + ' hi'; } // 外层无法直接赋值,通过 set 赋值 set 阅读全文
posted @ 2020-06-04 06:45 wzndkj 阅读(1612) 评论(0) 推荐(0)
摘要:访问类型有 private, protected, public class Person { name: string; public sayHi() { console.log(this.name); // 类内调用 } } class Teacher extends Person{ publi 阅读全文
posted @ 2020-06-03 06:55 wzndkj 阅读(565) 评论(0) 推荐(0)
摘要:class Person{ } 这就是 ts 里面最基础的类 class Person { name = 'zina'; getName() { return this.name } } // 有了类后创建一个实例 const person = new Person(); console.log(p 阅读全文
posted @ 2020-06-02 06:26 wzndkj 阅读(353) 评论(0) 推荐(0)
摘要:const getPersonName = person => { console.log(person.name); } const setPersonName = (person, name) => { person.name = name; } /** * 这是没用 ts 语法,这样会有问题, 阅读全文
posted @ 2020-06-01 06:56 wzndkj 阅读(292) 评论(0) 推荐(0)
摘要:/** * TS 数组和 JS 数组是一样的 * 变量提示,const numberArr: number[] */ const numberArr = [1, 2, 3]; const arr: number[] = [1,2,3]; const stringArr: string[] = ['a 阅读全文
posted @ 2020-05-29 06:44 wzndkj 阅读(1894) 评论(0) 推荐(0)
摘要:// JS 中定义函数的方式 function hell(){} const hello1 = function(){} const hello2 = () => {} // TS 中定义函数的方式 // 参数需要类型注解,返回值不需要 function add(first: number, sec 阅读全文
posted @ 2020-05-27 20:18 wzndkj 阅读(199) 评论(0) 推荐(0)
摘要:// type annotation 类型注解,我们来告诉 TS 变量是什么类型 // type inference 类型推断,TS 会自动的去尝试分析变量的类型 // 如果 TS 能够自动分析变量类型,我们就什么也不需要做了 // 如果 TS 无法分析变量类型的话,我们就需要使用类型注解 let 阅读全文
posted @ 2020-05-27 06:24 wzndkj 阅读(932) 评论(0) 推荐(0)
摘要:// 基础类型 null, undefined, symbol, boolean, void const count: number = 123; const teacherName: string = 'zina'; *:如果基础类型是一行的,ts 能推断出它的类型,如果是两行就不一定了 let 阅读全文
posted @ 2020-05-26 06:34 wzndkj 阅读(4704) 评论(1) 推荐(0)
摘要:学 TypeScript 就是在学 TypeScript 的静态类型和 JavaScript 的衍生语法 const count: number = 2019; // 按照之前的理解,count 是个 number 类型的静态类型。不能变更为其他类型 count.toFixed(); // coun 阅读全文
posted @ 2020-05-25 06:31 wzndkj 阅读(1813) 评论(0) 推荐(0)
摘要:要运行 TypeScript,需要一个 node 的运行环境, 1、安装 node,http://nodejs.cn/download/, 输入 node -v, npm -v 能争取输出版本好, node 安装就没有问题了 2、配置 vsCode,打开 Code - Preferences - S 阅读全文
posted @ 2020-05-21 06:56 wzndkj 阅读(159) 评论(0) 推荐(0)
摘要:demo.js function demo(data) { return Math.sqrt(data.x ** 2 + data.y ** 2); } demo(); demo.ts function tsDemo(data:{x: number, y: number}) { return Mat 阅读全文
posted @ 2020-05-20 06:48 wzndkj 阅读(1041) 评论(0) 推荐(0)
摘要:TypeScript 是 JavaScript 的超集,正因为它是建立在 JavaScript 上的一门语言,TypeScript 把其他语言中的一些精妙的语法带入到了 JavaScript 之中,从而把 JS 带入到了一个新的高度,在 TS 里面可以使用各种 JS 之外的扩展语法,同时 TS 对面 阅读全文
posted @ 2020-05-19 07:02 wzndkj 阅读(327) 评论(0) 推荐(0)
摘要:模块:模块可以帮助开发者将代码分割为重用的单元。开发者可以自己决定将模块中的哪些资源(类,方法,变量)暴露出去供外部使用,哪些资源只在模块内使用 在ts里面,一个文件就是一个模块,并没有什么特殊的标识。在模块的内部有两个关键字来支撑模块的特性,这两个特性就是export 和 import a.ts 阅读全文
posted @ 2019-10-13 11:21 wzndkj 阅读(236) 评论(0) 推荐(0)
摘要:接口:用来建立某种代码约定,使得其他开发者在调用某个方法或创建新的类时必须遵循接口所定义的代码约定 在js里面没有接口这个概念,在ts里面通过两个关键字来支撑接口这个特性 interface interface IPerson { name: string; age: number; } 在接口里面 阅读全文
posted @ 2019-10-13 10:57 wzndkj 阅读(162) 评论(0) 推荐(0)
摘要:泛型:参数化的类型,一般用来限制集合的内容 class Person { constructor(public name:string) { } eat() { console.log(this.name) } } var workers: Array<Person> = []; 这里的<Perso 阅读全文
posted @ 2019-10-13 10:40 wzndkj 阅读(110) 评论(0) 推荐(0)
只有注册用户登录后才能阅读该文。
posted @ 2019-10-12 07:00 wzndkj 阅读(5) 评论(0) 推荐(0)