type Texta = {
// 别名 与接口的区别 接口只能赋值对象 而 别名可以赋值类型
text:string,
time: number
}
interface Xiao {
name:string,
age:number,
[things:string]: any, // 使用这个接口属性 可以任意在实现的对象上面 添加属性
say?:(obj:Texta)=>void // 接口函数 使用别名规范入参 如果你在函数传参 使用拓展运算符一定记得
}
let arr:Array<Xiao> = [] // 表示数组的每一项都是 符合接口的 对象数据结构
function getPerson({
name,age}:Xiao):Array<Xiao>{
arr[0] = {
name,age}
return arr
} // 规定了 入参 解构类型
let xiao:Xiao = {
name:'小明',
age: 12
}
getPerson({
...xiao})
class Pe {
hello () {
}
}
class Person extends Pe {
public name:string // 定义变量
private age:number // 年龄保密 谁也不告诉
protected money: number // 财产公开
static sex:string // 性别一辈子不变
constructor () {
super()
}
hello () {
// 重写方法
super.hello() // 内部调用
}
}
type Perso = {
// 别名
name:string,
age:number,
sex?: number // 可选
}
enum sexType {
// 枚举
m = 1,
w = 0
} // 规定变量类型 可以 使用 类 接口 别名 来限制
type arrayItem = string | number // 别名规定
let ar:arrayItem[] = ['12',12] // 规定数组
let yuan: [string,string,number] = ['1','1',1] // 元组
let br:Perso = {
// 用别名限制变量 使用枚举来 表面类型
name: '12',
age: 12,
sex: sexType.m
}
let cr:Xiao = {
// 用别名限制变量
name:'12',
age:12,
xin: '心动了',
xina: '心动了',
say: function({
}:Texta) {
}
}
cr.say({
text:'12',time:12})
type funItem = () => void // 函数
let er:funItem = function () {
}
er()
namespace My {
// 命名空间
export let a:number = 1
export let b:()=>void = function () {
}
}
let b:number = My.a
let c:object = new My.b() // 构造函数
interface arrLen {
// 接口 来 实现泛型类 因为 存在 number 取不到length属性
length:number
}
type arra = (number | string) []
let ae:arra = [1,2,1]
class Len <T extends arrLen> {
constructor () {
}
arr(ar:T){
return ar.length
}
}
const a = new Len<string>()
初学typescript第二版
猜你喜欢
转载自blog.csdn.net/qq_43505774/article/details/113522503
今日推荐
周排行