首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >专栏 >TS STRUCTURE - Basic TS Types

TS STRUCTURE - Basic TS Types

作者头像
Cellinlab
发布于 2023-05-17 11:07:54
发布于 2023-05-17 11:07:54
54700
代码可运行
举报
文章被收录于专栏:Cellinlab's BlogCellinlab's Blog
运行总次数:0
代码可运行

# Boolean

代码语言:javascript
代码运行次数:0
运行
AI代码解释
复制
let isAlive: boolean = false;

# Number

A variable with the number data type can contain anynumeric literal with float-ing, hexadecimal, and binary or octal values.

代码语言:javascript
代码运行次数:0
运行
AI代码解释
复制
let decimal: number = 6;
let hex: number = 0xf00d;
let binary: number = 0b1010;
let octal: number = 0o744;

# String

代码语言:javascript
代码运行次数:0
运行
AI代码解释
复制
let name: string = "Cell";
name = 'cellinlab';

let sentence: string = `There is ${name}'s blog.`;

# Array

代码语言:javascript
代码运行次数:0
运行
AI代码解释
复制
let list: number[] = [1, 2, 3];

let list2: Array<number> = [1, 2, 3];

# Tuple

The Tuple type gives you the ability to declare an array with a known fixed number of elements that do not have to be of the same type.

代码语言:javascript
代码运行次数:0
运行
AI代码解释
复制
let x: [string, number];

x = ['hello', 2022]; // OK
// x = [2022, 'hello']; // Error

console.log(x[0].substr(1)); // OK
// console.log(x[1].substr(1)); // Error, 'number' does not have 'substr'

x[3] = 'cellinlab'; // OK, string type can be assigned to '(string | number)'

console.log(x[5].toString()); // OK, 'string' and 'number' both have the 'toString' method

// x[6] = true; // Error, 'boolean' cannot be assigned to '(string | number)'

# Tuples Deconstruction

Since tuples use array syntax, they can be deconstructed or disassembled in two ways.

代码语言:javascript
代码运行次数:0
运行
AI代码解释
复制
console.log(`tupleType[0] = ${tupleType[0]}`);
console.log(`tupleType[1] = ${tupleType[1]}`);

[t1, t2] = tupleType;
console.log(`t1 = ${t1}`);
console.log(`t2 = ${t2}`);

# Optional Tuple Elements

Like function signatures, we can also have optional tuple elements.

代码语言:javascript
代码运行次数:0
运行
AI代码解释
复制
let optionalTuple: [string, boolean?];

optionalTuple = ['hello', true];
console.log(`optionalTuple: ${optionalTuple}`); // optionalTuple: 'hello', true

optionalTuple = ['cell'];
console.log(`optionalTuple: ${optionalTuple}`); // optionalTuple: 'cell'

# Enum

Enum is a special type borrowed from other languages like C#, C ++, and Java that provides a solution to the special numbers problem. Enum binds a human-readable name for a specific number.

代码语言:javascript
代码运行次数:0
运行
AI代码解释
复制
enum Color {Red, Green, Blue};

let c: Color = Color.Green;

By default, enums start with 0. You can change this by directly specifying a value for one of the enum members.

代码语言:javascript
代码运行次数:0
运行
AI代码解释
复制
enum Color {Red = 1, Green, Blue};

let c: Color = Color.Green;

Or even set values for all members:

代码语言:javascript
代码运行次数:0
运行
AI代码解释
复制
enum Color {Red = 1, Green = 2, Blue = 4};

let c: Color = Color.Green;

A convenient feature of enumerations is that you can also get the name of an enumeration member by passing its numeric value.

代码语言:javascript
代码运行次数:0
运行
AI代码解释
复制
enum Color {Red = 1, Green = 2, Blue = 4};

let colorName: string = Color[2];
console.log(`colorName: ${colorName}`); // colorName: Green

Enumerations are a convenient way to define an easy-to-remember, easy-to-read name for a special number.

代码语言:javascript
代码运行次数:0
运行
AI代码解释
复制
enum DoorState {Open, Closed, Ajar};

let openDoor: DoorState = DoorState.Open;
console.log(`openDoor: ${openDoor}`); // openDoor: 0

let closedDoor: DoorState = DoorState['Closed'];
console.log(`closedDoor: ${closedDoor}`); // closedDoor: 1

# String Enum

Another variant of the enum type is a string enumeration, in which numeric values are replaced with strings.

代码语言:javascript
代码运行次数:0
运行
AI代码解释
复制
enum DoorStateString {
  Open = 'open',
  Closed = 'closed',
  Ajar = 'ajar'
};

let openDoorString: DoorStateString = DoorStateString.Open;
console.log(`openDoorString: ${openDoorString}`); // openDoorString: open

# Any

We may need to describe the type of variables that wedon’t know when we write our application. These values can be obtained from dynamic content, such as from a user or from a third-party library. In these cases, we want to disable type checking and allow the values to pass validation at compile time.

代码语言:javascript
代码运行次数:0
运行
AI代码解释
复制
let notSure: any = 4;
notSure = 'maybe a string instead';
notSure = false; // okay, definitely a boolean

The any type can also be useful if you know some part of the variable type, but not all of it.

代码语言:javascript
代码运行次数:0
运行
AI代码解释
复制
let list: any[] = [1, true, 'free'];
list[1] = 100;

# Void

Void is the opposite of Any: the absence of any types. It is most often used as the return type of functions that do not return any value.

代码语言:javascript
代码运行次数:0
运行
AI代码解释
复制
function warnUser(): void {
  console.log('This is my warning message');
}

Declaring variables with the void type is useless, because you can only assign them undefined or null values:

代码语言:javascript
代码运行次数:0
运行
AI代码解释
复制
let unusable: void = undefined;

# Null and Undefined

The Null and Undefined types correspond to the same types in JS. These types are subtypes for all other types by default.

代码语言:javascript
代码运行次数:0
运行
AI代码解释
复制
let n: number = null; // Primitive types can be null
n = undefined; // Primitive types can be undefined

let x = null; // same as x: any = null
let y = undefined; // same as y: any = undefined

// let e: Null; // Error
// let f: Undefined; // Error

If you declare a variable of type null or undefined, then such a variable can only be assigned the value null or undefined, respectively, which has no practical application.

代码语言:javascript
代码运行次数:0
运行
AI代码解释
复制
let n: null = null;

// n = 1; // Error

let u: undefined = undefined;
// u = 'cellinlab'; // Error

In this case, if the variable needs to be assigned a value with the string or null or undefined type, you can use the string | null | undefined union type.

# Never

The never type represents a type whose value never occurs. For example, never is a type that returns a function that always throws exceptions or that never exits (for example, an infinite loop). Variables can also have this type, for example, in order to never take the value true.

The never type is a subtype of any type. A variable of type never can be assigned to a variable of any other type. On the other hand, there is no such type that will be a subtype of this type, just as a variable of this type cannot be assigned anything other than a variable of the same type (never).

代码语言:javascript
代码运行次数:0
运行
AI代码解释
复制
function error(message: string): never {
  throw new Error(message);
}

// the output type of fail() is never
function fail() {
  return error('Something failed');
}

// no exit from this function
function infiniteLoop(): never {
  while (true) {
  }
}

# Symbol

The Symbol type is primitive and corresponds to the same type in JS.

This type provides unique identifiers that can be used askeys for object properties.

代码语言:javascript
代码运行次数:0
运行
AI代码解释
复制
let secretKey = Symbol();

let obj = {};

obj[secretKey] = 'secret value'; // Symbol as property
obj[Symbol.toStringTag] = 'test';

# Type Assertions

Sometimes you find yourself in a situation where you know more about the value of a variable than TS does.This usually happens when you know that the type of an entity may be more specific than its current type.

Type assertion is like typecasting in other languages, but it doesn’t do any special checks or data restructurings.The type conversion has no effect at the execution stage of the program and is used only by the compiler. TS assumes that the programmer will do all the necessary checks that are required.

The type conversion can be done in two ways. The first is the use of angle brackets syntax:

代码语言:javascript
代码运行次数:0
运行
AI代码解释
复制
let someValue: any = 'this is a string';
let strLength: number = (<string>someValue).length;

The second is as-syntax:

代码语言:javascript
代码运行次数:0
运行
AI代码解释
复制
let someValue: any = 'this is a string';
let strLength: number = (someValue as string).length;

# Destructuring

代码语言:javascript
代码运行次数:0
运行
AI代码解释
复制
let input = [1, 2];
let [first, second] = input;
console.log(`first: ${first}, second: ${second}`); // first: 1, second: 2

function f ([first, second]: [number, number]) {
  console.log(`first: ${first}, second: ${second}`);
}

You can also destruct objects.

代码语言:javascript
代码运行次数:0
运行
AI代码解释
复制
let o = {
  a: 'foo',
  b: 12,
  c: 'bar'
};

let { a, b } = o;
console.log(`a: ${a}, b: ${b}`); // a: foo, b: 12

// You can also give different names to the properties.
let { a: newName1, b: newName2 } = o;
console.log(`newName1: ${newName1}, newName2: ${newName2}`); // newName1: foo, newName2: 12

# Default Values

Default values allow you to define a property, even if it was not set.

代码语言:javascript
代码运行次数:0
运行
AI代码解释
复制
function keepWholeObject(wholeObject: { a: string, b?: number }) {
  let { a, b = 1001 } = wholeObject;
}

# Declaring Functions

代码语言:javascript
代码运行次数:0
运行
AI代码解释
复制
type C = { a: string, b?: number };

function f({ a, b }: C): void {
  console.log(`a: ${a}, b: ${b}`);
}

// Specifying default values
function f2 ({ a, b } = { a: '', b: 0 }): void {
  console.log(`a: ${a}, b: ${b}`);
}
f2() // a: , b: 0
本文参与 腾讯云自媒体同步曝光计划,分享自作者个人站点/博客。
原始发表:2022/4/18,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体同步曝光计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
暂无评论
推荐阅读
[微服务]BPMN和微服务编排,流程语言,引擎和永恒模式(第1部分)
我们正在构建Zeebe作为下一代工作流引擎,用于新兴用例,例如微服务编排用例,这些用例可能需要引擎每秒处理数十万(或数百万)个新工作流实例。
架构师研究会
2019/05/06
3.4K0
[微服务]BPMN和微服务编排,流程语言,引擎和永恒模式(第1部分)
一文读懂微服务编排利器—Zeebe
导语 | 微服务架构的一大核心是把大的复杂的业务系统拆分成高内聚的微服务,每个服务负责相对独立的逻辑。服务拆分的好处无需赘述,但是要实现业务价值,不是看单个服务的能力,而是要协调所有服务保证企业端到端业务流的成功。那么,谁来负责端到端业务流的成功呢?在调研工作流引擎的过程中,笔者了解到微服务编排模式及微服务编排引擎Zeebe,可以很好的回答这个问题。文章作者:唐炯,腾讯CSIG研发工程师。 一、工作流与微服务编排 1. 工作流 提到工作流,印象里都是OA系统各种请假审批流。事实上,广义上的工作流是
腾讯云开发者
2021/03/25
6.5K1
微服务——选择的架构
微服务体系结构与更传统的单块开发风格的区别在于必须做出的选择的数量。您将使用哪些框架(如果有的话)?如何处理配置,编制或编排等等。它可能觉得不知所措。在这篇文章中,我将给你一些建议如何处理这个架构的选
程序你好
2018/07/23
4620
微服务——选择的架构
为什么选择工作流引擎?三大主流引擎优缺点剖析
工作流引擎是一种软件系统,用于自动化、管理和监控业务流程的逻辑执行。它通过预定义的规则和流程模型,协调任务在不同角色、系统之间的流转,确保流程按既定路径高效完成。其核心功能包括:
没事学点编程小知识
2025/03/04
4751
为什么选择工作流引擎?三大主流引擎优缺点剖析
「首席架构师推荐」工作流引擎哪家强?首席架构帮你挑
原文:https://github.com/meirwah/awesome-workflow-engines
架构师研究会
2019/09/25
4.7K0
「首席架构师推荐」工作流引擎哪家强?首席架构帮你挑
微服务中数据CQRS操作的事务处理
本文的主要主题是描述如何使用事件源(event sourcing)和CQRS将事件驱动的体系结构与微服务集成。
程序你好
2019/03/08
1.3K0
云原生时代的业务流程编排
既然今天要聊一聊云原生时代的业务流程编排,那咱们首先得定义什么是流程编排以及传统的流程编排是做什么的。传统的流程编排一般分两类:bussiness process management(BPM 业务流程管理)和 workflow engine (工作流引擎),在过去十几年里,商业领域主要是以BPM为主,软件服务厂商以平台化的产品为企业客户提供流程设计、流程管理、流程自动化相关的能力。
jesseai
2020/02/22
15.5K5
云原生时代的业务流程编排
工作流引擎技术方案<初版>
Vue Flow(核心) + Dagre(布局) + Vuedraggable(拖拽) + Vue 3 Composition API(架构)
舒一笑不秃头
2025/06/25
2050
工作流引擎技术方案<初版>
分布式微服务流程编排简介
微服务的流程编排将成为下一个要解决的大问题。在撰写本文时,有几种解决方案试图在该领域竞争,主要是构建自己的(文本)领域特定语言来描述业务流程。在我看来,编排应该改为在BPMN 2.x中表达,因为它是为此目的而精心设计的,易于理解且成熟的语言。
lyb-geek
2019/11/20
1.7K0
微服务的创建和管理最常见问题是什么?
如果你不正确地划分责任,你就会遇到问题。将任何应用程序应用到分布式系统中。想想你可能会遇到的问题。管理数据和管理状态是许多人在管理有状态和无状态数据时遇到问题的地方。考虑事件驱动的命令和数据通信,以构建最好的体系结构。
程序你好
2018/12/05
8310
golang办公工作流workflow js-ojus/flow包介绍——系列一
golang办公工作流workflow利用js-ojus/flow做测试——系列二
hotqin888
2022/05/07
2.3K0
微服务数据一致性的演进:SAGA,CQRS,Event Sourcing的由来和局限
原题:Data consistency in microservices architecture
yuanyi928
2018/11/30
2.5K0
微服务数据一致性的演进:SAGA,CQRS,Event Sourcing的由来和局限
「首席架构师推荐」精选的开源工作流引擎列表,
原文:https://github.com/meirwah/awesome-workflow-engines
架构师研究会
2019/09/25
2.8K0
「首席架构师推荐」精选的开源工作流引擎列表,
微服务体系结构——学习、构建和部署应用程序
更好地理解微服务架构,并举例这种架构好处,以及Uber如何将它们的单体应用变成微型服务。
程序你好
2018/07/23
5850
微服务编排之道
目录: 一、微服务需要编排吗? 二、微服务编排的流程 三、微服务编排的一致性 四、微服务编排的监控工具支撑 一、微服务需要编排吗? 微服务是一种新的软件架构风格。在微服务体系结构中,可以将应用分解为多
yuanyi928
2018/03/30
6.8K1
微服务编排之道
没有工作流是孤岛
Dapr 的统一 API 和模式,包括跨语言和框架的工作流,解放了开发者面对碎片化技术的困扰。
云云众生s
2024/03/28
1930
没有工作流是孤岛
golang语言的办公工作流的包
版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/hotqin888/article/details/78822774
hotqin888
2018/09/11
2.4K0
如何从传统单体架构转向微服务
当今,把单体架构的应用拆成微型服务是很时髦的。让我想起了2000年世纪初的那些日子,那时SOA正在流行,大多数公司,供应商和系统集成商,正忙着挥动SOA魔杖,希望它能将他们的遗留应用程序转变为更加灵活和敏捷的SOA应用程序。一个供应商甚至说,“使用SOA,您不需要编写一行代码“。不幸的是,事实却根本不是这样。虽然他们的大肆宣传,努力去做,结果却不美好。虽然服务的概念还不错,但是SOA具有强类型的服务定义,并且在HTTP上使用SOAP非常麻烦。这些缺点似于谚语中所说的“当你有一个新的闪亮的锤子时,一切看起来都像钉子”,这就是SOA的末日。
程序你好
2018/05/24
2.1K0
如何从传统单体架构转向微服务
企业级BPM之微服务架构演进
BPM平台在各行业的IT架构中都是重要的基础支撑平台,十二五期间,企业级BPM作为SOA体系下的关键组件,经历了一个加速建设的过程。我们也有幸参与了一些行业的流程平台建设,今天与大家分享我们在流程引擎
yuanyi928
2018/04/02
2.7K0
企业级BPM之微服务架构演进
事件驱动微服务体系架构
如果您是一名企业架构师,您可能听说过微服务架构,并使用过它。虽然您过去可能使用REST作为服务通信层,但是越来越多的项目正在转向事件驱动的体系结构。让我们深入了解这种流行架构的优缺点、它所包含的一些关键设计选择以及常见的反模式。
程序你好
2019/11/06
1.7K0
推荐阅读
相关推荐
[微服务]BPMN和微服务编排,流程语言,引擎和永恒模式(第1部分)
更多 >
LV.1
这个人很懒,什么都没有留下~
目录
  • # Boolean
  • # Number
  • # String
  • # Array
  • # Tuple
    • # Tuples Deconstruction
    • # Optional Tuple Elements
  • # Enum
    • # String Enum
  • # Any
  • # Void
  • # Null and Undefined
  • # Never
  • # Symbol
  • # Type Assertions
  • # Destructuring
  • # Default Values
  • # Declaring Functions
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档