// 枚举
export function MeiJu() {
enum a { "blue", "black" }
const b: a = a.blue
console.log("枚举", b)
}
export function AnyTest() {
// any 不明确的类型
let a: any = "你好"
console.log("不明确类型string", a)
a = false;
console.log("不明确类型布尔", a)
}
export function ArrayTest() {
// 数组声明
const a: Array<number> = [1, 2, 3]
const b: string[] = ["哈哈哈", "aaa"]
for (const bb in b) {
console.log("数组循环", bb)
}
// 数组 转换 普通数组
const c = Array.from(b)
console.log("数组 转换 普通数组", c)
// 数组 迭代
for (const d in b) {
console.log("数组迭代", d)
}
}
export function YuanZu() {
const d: [string, number] = ["小明", 12]
console.log("元组", d[0])
console.log("元组", d[1])
// 解构元组
const [name, age] = d;
console.log("元组解构name", name)
console.log("元组解构age", age)
// 元组连接
const e: [number, string] = [12, "er"]
const f = d.concat(e)
console.log(f)
// 元组 切片
const g = f.slice(1)
console.log("元组切片", g)
// 元组遍历
for (const g1 in g) {
console.log("元组切片迭代", g1)
}
// 元组转换 普通数组
const k = Array.from(g)
console.log("元组转换普通数组", k)
// 扩展元组
const m: [string, number] = ["aaa", 1]
const n: [string, number] = ["bbb", 2]
const p: [string, number, ...typeof n] = [...m, ...n]
console.log("元组展开", p)
}
// 联合类型
export function LianHe() {
let a: string | number | boolean = "1"
a = false
console.log("联合类型 布尔赋值", a)
a = "好好"
console.log("联合类型 字符串赋值", a)
let b: string[] | number[] | boolean[] = ["aa", "bb", "cc"];
console.log("联合类型字符串数组", b)
b = [false, false]
console.log("联合类型布尔数组", b)
}
// 接口
interface IPoint {
x: number
y: number
}
function AddPoint(p1: IPoint, p2: IPoint): IPoint {
const x = p1.x
const y = p2.y
return { x: x, y: y }
}
AddPoint({ x: 1, y: 2 }, { x: 3, y: 4 })
class Point implements IPoint {
x = 10;
y = 20;
// 静态变量
static c = 30
constructor(x: number, y: number) {
this.x = x
this.y = y
}
// 实例方法
ShowPoint() {
return this.x + this.y
}
// 静态方法
static StaicShowPoint() {
return Point.c
}
}
export const point = new Point(10, 20)
interface KeyValuePair<k, v> {
key: k
value: v
}
function FaxingHanShu<T>(arg: T) {
console.log("泛型函数", arg)
}
FaxingHanShu<string>("你好泛型函数")
export function FanxingDefault<T = string>(arg: T) {
console.log("泛型约束默认值", arg)
}
// 泛型接口
interface FanxingInterface<T> {
first: T
}
const Fanxing: FanxingInterface<string> = { first: "第一" }
class FanxingLeiShiXian implements FanxingInterface<string> {
first = "你好"
constructor(first: string) {
this.first = first
}
GetFirst() {
return this.first
}
}
class FnxingLei<T> {
private name: T
constructor(name: T) {
this.name = name
}
public GetName() {
console.log("泛型类", this.name)
return this.name
}
}
export const fanxingLei = new FnxingLei<string>("小明")
import { MeiJu, AnyTest, ArrayTest, YuanZu, LianHe, point, fanxingLei, FanxingDefault } from './assets/js/testts1'
fanxingLei.GetName()
FanxingDefault<number>(12)
MeiJu()
AnyTest()
ArrayTest()
YuanZu()
LianHe()
point.ShowPoint()
![]()