We will look at how we can use mapped types, conditional types, self-referencing types and the “infer” keyword to create a reusable generic type that traverses down the properties of an object and marks of all them as read-only. This is especially us…
With properties we can follow a one-way parent→child flow communication between components. This lesson shows you how you can pass down properties to class based Vue components by using the @Prop decorator from vue-property-decorator. We’ll also see…
Sometimes we might want to make a function more generic by having it accept a union of different types as an argument. Using the JavaScript “in” operator, we can test for the presence of different properties on the argument object, and TypeScript wil…
转自:https://www.olioapps.com/blog/checking-types-real-world-typescript/ This is a follow-up to Type-Driven Development with TypeScript. The shape of data defines a program. There are important benefits to writing out types for your data. Let’s conside…
简介 JavaScript语言基于函数和原型链继承机制的方式构建可重用的组件.这对于OO方面编程来说显得比较笨拙.在下一代的JavaScript标准ECMAScript 6为我们提供了基于class base的OO设计方式.在TypeScript中我们也允许使用这种方式,TypeScript将编译为目前大多数浏览器能允许的普通Javascript代码,所以我们不用在等待ECMAScript 6的到来了. 类 我们先看一个关于class-base的实例: class Greeter { greet…
TypeScript中的怪语法 如何处理undefined 和 null undefined的含义是:一个变量没有初始化. null的含义是:一个变量的值是空. undefined 和 null 的最佳实践 核心思想: 避免null pointer错误. null is bad. 要避免这个问题,我们需要做到: 用undefined,不要用null. 根据Code guidelines from Microsoft. Enable "strict" 或者 "strictNul…
一.模仿Reddit a) 运行ng new –ng4angular-reddit创建应用,从随书代码中复制样式文件,新建组件app-root,代码为: 界面可以看到了: b) 对于界面输入的数据,获取的方式有点特别,使用了#newlink这样的语法,newlink是一个对象,现在代表就是所在的input这个DOM元素. 将对象作为参数传递给addArticle方法,在对应的ts代码中,可以获取newlink.value.newlink是HTMLInputElement类型. c)关于参数的绑定…
枚举部分 Enumeration part 使用枚举我们可以定义一些有名字的数字常量. 枚举通过 enum关键字来定义. Using enumerations, we can define some numeric constants with names. Enumeration is defined by the enum keyword. enum Direction { Up = 1, Down, Left, Right } 枚举是在运行时真正存在的一个对象,其中一个原因是因为这样可以从…
原文:https://blog.oio.de/2014/03/21/declaration-merging-typescript/ Why might you need this? There can be several scenarios where this might be required. One of the most common ones is when you want to extend an existing JavaScript library that comes w…
本文转自:http://www.typescriptlang.org/docs/tutorial.html Quick start Get started with a simple TypeScript app. Let’s get started by building a simple web application with TypeScript. Installing TypeScript There are two main ways to get the TypeScript to…