TypeScript基本语法
内容导读
互联网集市收集整理的这篇技术教程文章主要介绍了TypeScript基本语法,小编现在分享给大家,供广大互联网技能从业者学习和参考。文章包含2654字,纯文字阅读大概需要4分钟。
内容图文
![TypeScript基本语法](/upload/InfoBanner/zyjiaocheng/1082/c5839783e28842d1bb49f9353df3ec89.jpg)
#1---接口
interface User {
name: string;
id: number;
}
const user: User = {
username: "Hayes",
id: 0,
};
#2---类
class UserAccount {
name: string;
id: number;
constructor(name: string, id: number) {
this.name = name;
this.id = id;
}
}
const user: User = new UserAccount("Murphy", 1);
#3---函数
function getAdminUser(): User {
//...
}
function deleteUser(user: User) {
// ...
}
#4---Composing Types:unions
type WindowStates = "open" | "closed" | "minimized";
type LockStates = "locked" | "unlocked";
type OddNumbersUnderTen = 1 | 3 | 5 | 7 | 9;
----处理不同类型的函数
function getLength(obj: string | string[]) {
return obj.length;
}
#5---判断类型
Type Predicate
string typeof s === "string"
number typeof n === "number"
boolean typeof b === "boolean"
undefined typeof undefined === "undefined"
function typeof f === "function"
array Array.isArray(a)
-----判断类型的应用
function wrapInArray(obj: string | string[]) {
if (typeof obj === "string") {
return [obj];
// ^ = (parameter) obj: string
} else {
return obj;
}
}
#6---Composing Types:Generics
type StringArray = Array<string>;
type NumberArray = Array<number>;
type ObjectWithNameArray = Array<{ name: string }>;
interface Backpack<Type> {
add: (obj: Type) => void;
get: () => Type;
}
// This line is a shortcut to tell TypeScript there is a
// constant called `backpack`, and to not worry about where it came from
declare const backpack: Backpack<string>;
// object is a string, because we declared it above as the variable part of Backpack
const object = backpack.get();
// Due to backpack variable being a string, you cannot pass a number to the add function
backpack.add(23);
#7---Structural Type System
interface Point {
x: number;
y: number;
}
function printPoint(p: Point) {
console.log(`${p.x}, ${p.y}`);
}
// prints "12, 26"
const point = { x: 12, y: 26 };
printPoint(point);
const point3 = { x: 12, y: 26, z: 89 };
printPoint(point3); // prints "12, 26"
const rect = { x: 33, y: 3, width: 30, height: 80 };
printPoint(rect); // prints "33, 3"
const color = { hex: "#187ABF" };
class VirtualPoint {
x: number;
y: number;
constructor(x: number, y: number) {
this.x = x;
this.y = y;
}
}
const newVPoint = new VirtualPoint(13, 56);
printPoint(newVPoint); // prints "13, 56"
// Declare a tuple type
let x: [string, number];
// Initialize it
x = ["hello", 10]; // OK
// OK
console.log(x[0].substring(1));
#8---枚举类型、enum Color
enum Color {
Red,
Green,
Blue,
}
let c: Color = Color.Green;
{
Red = 1,
Green = 2,
Blue = 4,
}
let c: Color = Color.Green;
enum Color {
Red = 1,
Green,
Blue,
}
let colorName: string = Color[2];
// Displays ‘Green‘
console.log(colorName);
原文:https://www.cnblogs.com/Lambquan/p/13534734.html
内容总结
以上是互联网集市为您收集整理的TypeScript基本语法全部内容,希望文章能够帮你解决TypeScript基本语法所遇到的程序开发问题。 如果觉得互联网集市技术教程内容还不错,欢迎将互联网集市网站推荐给程序员好友。
内容备注
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 gblab@vip.qq.com 举报,一经查实,本站将立刻删除。
内容手机端
扫描二维码推送至手机访问。