TypeScript语法中有哪些操作符?
TypeScript语法所用的操作符分别是可选链操作符、非空断言操作符、明确类型断言、空合并操作符和明确类型转换?下面编程教程网小编给大家简单各种操作符的基本运用!
可选链操作符
interface Person {
name: string;
address?: {
city: string;
};
}
const person: Person = {
name: "John",
};
const city = person.address?.city;
非空断言操作符
const element: HTMLElement | null = document.getElementById("my-element");
const width: number = element!.offsetWidth;
明确类型断言
const input: unknown = "27";
const value: number = <number>(<string>input).length;
空合并操作符
const value: number | null = null;
const defaultValue = 27;
const result = value ?? defaultValue;
明确类型转换
const value: string = "27";
const numberValue: number = Number(value);
以上是编程学习网小编为您介绍的“TypeScript语法中有哪些操作符?”的全面内容,想了解更多关于 vuejs 内容,请继续关注编程基础学习网。