TypeScript 4.1 Quick Start Tutorials

TypeScript 4.1 快速上手教程

https://typescript-41-quick-start-tutorials.xgqfrms.xyz

  1. Template Literal Types

"use strict"; /**
*
* @author xgqfrms
* @license MIT
* @copyright xgqfrms
* @created 2020-12-10
* @modified
*
* @description Template Literal Types / 模板文字类型
* @augments
* @example
* @link https://www.typescriptlang.org/docs/handbook/release-notes/typescript-4-1.html
* @link https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Template_literals
*
*/ const log = console.log; function setVerticalAlignment(color: "top" | "middle" | "bottom") {
log(`color =`, color);
// ...
} setVerticalAlignment(`top`);
// setVerticalAlignment(`left`);
// Argument of type '"left"' is not assignable to parameter of type '"top" | "middle" | "bottom"'.ts(2345) type NewOptions = {
[K in "noImplicitAny" | "strictNullChecks" | "strictFunctionTypes"]?: boolean;
}; // same as
type Options = {
noImplicitAny?: boolean,
strictNullChecks?: boolean,
strictFunctionTypes?: boolean
}; type World = "world";
type Greeting = `hello ${World}`;
// type Greeting = "hello world" type Color = "red" | "blue";
type Quantity = "one" | "two"; type SeussFish = `${Quantity | Color} fish`;
// type SeussFish = "one fish" | "two fish" | "red fish" | "blue fish" type VerticalAlignment = "top" | "middle" | "bottom";
type HorizontalAlignment = "left" | "center" | "right"; // Takes
// | "top-left" | "top-center" | "top-right"
// | "middle-left" | "middle-center" | "middle-right"
// | "bottom-left" | "bottom-center" | "bottom-right" declare function setAlignment(value: `${VerticalAlignment}-${HorizontalAlignment}`): void; setAlignment("top-left");
// setAlignment("top-middle");
// Argument of type '"top-middle"' is not assignable to parameter of type '"top-left" | "top-center" | "top-right" | "middle-left" | "middle-center" | "middle-right" | "bottom-left" | "bottom-center" | "bottom-right"'. type PropEventSource<T> = {
on(eventName: `${string & keyof T}Changed`, callback: () => void): void;
}; /// Create a "watched object" with an 'on' method, so that you can watch for changes to properties.
declare function makeWatchedObject<T>(obj: T): T & PropEventSource<T>; let person = makeWatchedObject({
firstName: "xgqfrms",
age: 18,
location: "shanghai",
}); person.on("firstNameChanged", () => {
console.log(`firstName was changed!`);
}); // person.on("firstName", () => {});
// Argument of type '"firstName"' is not assignable to parameter of type '"firstNameChanged" | "ageChanged" | "locationChanged"'. // person.on("frstNameChanged", () => {});
// Argument of type '"frstNameChanged"' is not assignable to parameter of type '"firstNameChanged" | "ageChanged" | "locationChanged"'. type PropEventSource2<T> = {
on<K extends string & keyof T> (eventName: `${K}Changed`, callback: (newValue: T[K]) => void ): void;
}; declare function makeWatchedObject2<T>(obj: T): T & PropEventSource2<T>; let person2 = makeWatchedObject2({
firstName: "xgqfrms",
age: 18,
location: "shanghai",
}); // works! 'newName' is typed as 'string'
person2.on("firstNameChanged", newName => {
// 'newName' has the type of 'firstName'
console.log(`new name is ${newName.toUpperCase()}`);
}); // works! 'newAge' is typed as 'number'
person2.on("ageChanged", newAge => {
if (newAge < 0) {
console.log("warning! negative age");
}
}) type EnthusiasticGreetingUp<T extends string> = `${Uppercase<T>}`
type EnthusiasticGreetingLow<T extends string> = `${Lowercase<T>}` type HELLO = EnthusiasticGreetingUp<"hello">;
type hello = EnthusiasticGreetingLow<"HELLO">; let HOT: HELLO = `HELLO`;
let hot: hello = `hello`; log(`HELLO =`, HOT)
log(`hello =`, hot) export default setVerticalAlignment; export {
setVerticalAlignment,
setAlignment,
NewOptions,
Options,
Greeting,
SeussFish,
HELLO,
hello,
};
  1. Key Remapping in Mapped Types

  1. Recursive Conditional Types

  1. Checked Indexed Accesses (--noUncheckedIndexedAccess)

  1. paths without baseUrl

  1. checkJs Implies allowJs

  1. React 17 JSX Factories

  1. Editor Support for the JSDoc @see Tag

Breaking Changes








refs

https://www.typescriptlang.org/docs/handbook/release-notes/typescript-4-1.html

##

https://github.com/microsoft/TypeScript-Website/blob/v2/packages/documentation/copy/en/release-notes/TypeScript 4.1.md

https://raw.githubusercontent.com/microsoft/TypeScript-Website/v2/packages/documentation/copy/en/release-notes/TypeScript 4.1.md



xgqfrms 2012-2020

www.cnblogs.com 发布文章使用:只允许注册用户才可以访问!

原创文章,版权所有️xgqfrms, 禁止转载 ️,侵权必究️!


TypeScript 4.1 Quick Start Tutorials的更多相关文章

  1. webpack——Modules && Hot Module Replacement

    blog:JavaScript Module Systems Showdown: CommonJS vs AMD vs ES2015 官网链接: Modules 官网链接:Hot Module Rep ...

  2. 没有JavaScript的基础,我可以学习Angular2吗?

    Can I learn and understand Angular2 without understanding JavaScript? 没有JavaScript基础我能学习和理解Angular2吗 ...

  3. ROS_Kinetic_x 目前已更新的常用機器人資料 rosbridge agvs pioneer_teleop nao TurtleBot

    Running Rosbridge Description: This tutorial shows you how to launch a rosbridge server and talk to ...

  4. TurtleBot教程

    TurtleBot TurtleBot combines popular off-the-shelf robot components like the iRobot Create, Yujin Ro ...

  5. [转]TypeScript Quick start

    本文转自:http://www.typescriptlang.org/docs/tutorial.html Quick start Get started with a simple TypeScri ...

  6. TypeScript 4.x Tutorials

    TypeScript 4.x Tutorials TypeScript 4.x 最新教程 https://typescript-4x-tutorials.xgqfrms.xyz/ https://gi ...

  7. AngularJS2.0 quick start——其和typescript结合需要额外依赖

    AngularJS2 发布于2016年9月份,它是基于ES6来开发的. 运行条件! 由于目前各种环境(浏览器或 Node)暂不支持ES6的代码,所以需要一些shim和polyfill(IE需要)让ES ...

  8. SlickUpload Quick Start Guide

    Quick Start Guide The SlickUpload quick start demonstrates how to install SlickUpload in a new or ex ...

  9. QUICK START GUIDE

    QUICK START GUIDE This page is a guide aimed at helping anyone set up a cheap radio scanner based on ...

随机推荐

  1. 向HDFS中上传任意文本文件,如果指定的文件在HDFS中已经存在,由用户指定是追加到原有文件末尾还是覆盖原有的文件

    1 import java.io.FileInputStream; 2 import java.io.IOException; 3 import java.util.Scanner; 4 5 impo ...

  2. Google performance Tools (gperftools) 使用心得

    Google performance Tools (gperftools) 使用心得 gperftools是google开发的一款非常实用的工具集,主要包括:性能优异的malloc free内存分配器 ...

  3. 【题解】洛谷P3119 Grass Cownoisseur G

    题面:洛谷P3119 Grass Cownoisseur G 本人最近在熟悉Tarjan的题,刷了几道蓝题后,我飘了 趾高气扬地点开这道紫题,我一瞅: 哎呦!这不是分层图吗? 突然就更飘了~~~ 用时 ...

  4. Golang--Directional Channel(定向通道)

    Directional Channel 通道可以是定向的(directional).在默认情况下,通道将以双向的(bidirectional)形式运作,用户既可以把值放人通道,也可以从通道取出值;但是 ...

  5. AutoMapper源码解析

    研究AutoMapper源码前,我们先来看一下AutoMapper的作用 官网解释:AutoMapper是一个简单的小程序库,旨在解决看似复杂的问题-摆脱将一个对象映射到另一个对象的代码 解释 首先一 ...

  6. zabbix监控设备结果异常问题

    1.现象描述 paloalto防火墙升级后发现zabbix监控其CPU.风扇等硬件信息和端口流量数据错误. 2.现象分析 由于zabbix并没有做过任何调整,防火墙也只是升级.根据现象并不好排查.但是 ...

  7. DolphinScheduler 源码分析之 DAG类

    1 /* 2 * Licensed to the Apache Software Foundation (ASF) under one or more 3 * contributor license ...

  8. Java数组模拟队列 + 优化

    队列介绍 队列是一个有序列表,可以用数组或是链表来实现. 遵循先入先出的原则. 即:先存入队列的数据,要先取出.后存入的要后取出 示意图:(使用数组模拟队列示意图)  数组模拟队列 队列本身是有序列表 ...

  9. ElasticSearch结合Logstash(三)

    一.Logstash简介 1,什么是Logstash Logstash 是开源的服务器端数据处理管道,能够同时从多个来源采集数据,转换数据,然后将数据发送到您最喜欢的"存储库"中. ...

  10. 2020第十一届蓝桥杯第二场省赛C++A组【A-H】

    A. 门牌制作 答案 624 代码 #include <bits/stdc++.h> using namespace std; int main() { ios::sync_with_st ...