Typescript 查缺补漏

简介: Types Casting: let input = xxx as HTMLInputElement; let input = xxxx;   Object Shapes: Typescript only cares about the shape of an object.

Types

Casting:

let input = xxx as HTMLInputElement;
let input = <HTMLElement>xxxx;

 

Object Shapes:

Typescript only cares about the shape of an object.

 

Interfaces:

  • only describe structure, no implementation
  • don't compile to any js code
  • DRY, easy refactoring
  • open, can be extended later on. (extends)

 

any:

 

never:

Nothing can be assigned to never.

function return type void.

 

Classes

still protofypal inheritance, but better syntax.

 

static method:

static createPerson() {
}

 

instance / public fields:

class Person {
    static population = 122; // public (static) field
    country = 'China'; // instance field

    constructor(name) {
    }
}

 

inheritance:

class Person : Human {
    constructor() {
        super();
    }
}

super.xxx(); // function invoke.

 

Species:

    static get [Symbol.species]() {
        return Array;
    }

 

Mixins:

 

Enums:

enum Gender {
    Male,
    Female
}

 

 

Arrays:

 

Tuples:

Fixed length.

let t2: [string, number];
t2 = ['angular', 1];

 

 

Type Aliases:

interface sometimes is not good enough.

type keyword to define a type alias:

type Color = [number, number, number];
let red: Color = [255, 0, 0];

 

can be exported.

 

Object Literals

Enhanced:

let person = {
    __proto__ : String.prototype,
    name: 'Dave',
    company,
    [`${company}Title`]: 'CTO',
    toString() {
        return `${super.toString()}xxx`;
    }
};

 

 

Destructured Assignment:

 

Rest, Spread Properties:

...

rest: and the rest goes here

spread: and all the properties on this object.

 

Getters, Setters:

 

Function - Types:

Functions have a type just like any other value.

interface can also describe functions:

interface ClickListener {
    (this: Window, e: MouseEvent): void
}

const myListender: ClickListener = (e) => {
    console.log(e);
};

 

this, calling context must be window.

 

Function - Parameters:

named, optional, default value, rest parameters(...).

 

Generics

let persons = Array<[String, String]>(20);

 

can specify constraints on generic types:

function calc<T extends number>(x: T, y: T) {

}

 

can also be use with interface:

interface IFileReader<T extends File> {
    read(file: T): Blod
}

 

 

Access Modifier Keywords:

public, protected, private

 

Function overloading:

Allows us to have more than one function "head", but a single implementation.

function add(x: number, y: number): number; // this pattern ok
function add(x: string, y: string, radix: number): number; // this pattern ok

function add(x: number | string, y: number | string, radix: number = 10): number { // must match 2 patterns above.
    return parseInt(`${x}`, radix) + parseInt(`${y}`, radix);
}

 

add(1, '4'); // not ok

 

 

Iterators & Generators

Iterators: keeping track of current position, next()

Iterables

  • support for .. of loop.
  • Requires implementation of Symbol.iteractor method
  • Array, Map already support it.

 

Generators:

  • yield
  • function*() syntax
  • returns an iterator
  • State of closure is preserved between .next() calls.
  • Execution Can be Paused (yield).

 

it.next() goes in the loop, and pause after yield until next it.next() call.

 

Iterators:

The ability to pass values in while iterating.

console.log(it.next(134).value);

 

 

yield* keyword.

yield* [1, 2, 3];

 

下面是我的关于ASP.NET Core Web API相关技术的公众号--草根专栏:

目录
相关文章
|
前端开发 小程序
前端解析支付宝返回form表单,自动跳转支付
前端解析支付宝返回form表单,自动跳转支付
1202 1
|
11月前
|
存储 网络性能优化 网络安全
Hyper-V云桌面优化的设置指南
这份《Hyper-V云桌面优化设置指南》涵盖了硬件、虚拟机配置、网络、安全及高级配置的优化建议。硬件方面,确保CPU支持虚拟化并合理分配资源,使用SSD和VHDX格式提升存储性能;虚拟机配置上,优化内存、处理器和硬盘设置,并安装Hyper-V集成服务;网络优化包括虚拟交换机配置、适配器调整及QoS策略;安全性设置涵盖防火墙、端口管理和加密;高级配置如显卡直连和实时迁移进一步提升性能。这些措施能显著提高Hyper-V云桌面的效率与稳定性。
|
Java 测试技术 Maven
Maven打包使用多线程加速构建过程
Maven打包使用多线程加速构建过程
2892 0
|
Ubuntu Linux iOS开发
Linux (Debian 系) 安装官方微信 (Electron,非 wine 版)
最近才发现微信有推出基于 Electron.js 的一款桌面程序,不需要依赖 Wine 那复杂和冗余的依赖,只需要装一个稍微“大”一点的 deb 包就可以。
18165 0
|
编解码 安全 数据安全/隐私保护
速度与稳定性的完美结合:深入横测ToDesk、TeamViewer和AnyDesk
远程办公是指通过现代互联网技术,实现非本地办公:在家办公、异地办公、移动办公等远程办公模式。多数通过第三方远程控制软件来实现,例如通过远程手机/电脑远程控制办公设备进行工作或者会议,实现远程办公的目的。
2469 0
速度与稳定性的完美结合:深入横测ToDesk、TeamViewer和AnyDesk
|
弹性计算 数据可视化 搜索推荐
阿里云服务器镜像是什么意思?
阿里云服务器镜像是什么意思?镜像就是操作系统,镜像可以理解为云服务器的装机盘,镜像可选Linux系统或Windows操作系统,也可以选择预置了应用程序的镜像系统。
1171 0
阿里云服务器镜像是什么意思?
|
编解码 Java 测试技术
基于Sikuli GUI图像识别框架的PC客户端自动化测试实践
Sikuli是一款基于GUI图像识别框架的自动化测试工具,它以图像检索技术为基础,提供了一套基于 Jython 的脚本语言以及集成开发环境。使用者可利用屏幕截图直接 引用 GUI 元素进行编程,完成交互操作。Sikuli 一词取自墨西哥的土著语,意为“上帝之眼”,寓意—— Sikuli 让电脑能像人一样”看”这个”真实世 界”。
基于Sikuli GUI图像识别框架的PC客户端自动化测试实践
|
Android开发 Web App开发 索引
mac下使用android studio,解决无法打开问题。
1,下载最新的android studio http://developer.android.com/sdk/installing/index.html?pkg=studio mac下直接安装。但是启动不了,一直显示进度。 修改app应用权限。 菜单 System Preferences &gt; Security  下, 选择 select Anywhere。 2
2851 0
|
1天前
|
人工智能 自然语言处理 JavaScript
2026年Windows+Ollama本地部署OpenClaw保姆级教程:本地AI Agent+阿里云上快速搭建
2026年OpenClaw凭借本地部署、私有化运行的特性,成为打造个人智能体的核心工具,而Ollama作为轻量级本地大模型管理工具,能让OpenClaw摆脱对云端大模型的依赖,实现**本地推理、数据不泄露、全流程私有化**的智能体验。本文基于Windows 11系统,从硬件环境准备、Ollama安装与模型定制、OpenClaw部署配置、技能扩展到常见问题排查,打造保姆级本地部署教程,同时补充阿里云OpenClaw(Clawdbot)快速部署步骤,兼顾本地私有化需求与云端7×24小时运行需求,文中所有代码命令均可直接复制执行,确保零基础用户也能快速搭建属于自己的本地智能体。
3372 13

热门文章

最新文章