接口
interface Interface1 {
label: string;
value?:number;
readonly y: number;
readonly x?: number;
}
let obj:Interface1 = {
label:'label',
y:1
};
function func1(params:Interface1){
}
func1({label:'label',y:1})
func1({label:'label',y:1,
})
func1({label:'label',y:1,z:2 } as Interface1);
func1(<Interface1>{label:'label',y:1,z:2});
let p1 = {label:'label',y:1,z:2}
func1(p1)
interface Interface1 {
[name:string]:any
}
func1({label:'label',y:1,z:2})
interface Interface2 {
(code1: string, code2: string): boolean;
}
let func2:Interface2 = (name1:string,name2:string):boolean => true;
interface Interface3 {
name:string,
setName():void,
getName(name:string):string,
}
class Clock implements Interface3 {
name!:string;
constructor(){}
setName(){};
getName(name:string){
return name;
}
setAge(){};
}
let obj3:Interface3 = {
name:'a',
getName:(name:string) => name,
setName(){}
};
interface Interface4{
[type:number]:string
}
let arr1:Interface4 = ['a','b'];
console.log(arr1[0]);
interface Interface5 {
[index: string]: number;
length: number;
}
interface Interface6 {
readonly [index: number]: string;
}
let arrList1: Interface6 = ["haha", "hbhb"];
interface Interface7{
name:string
};
interface Interface8{
age:number
};
interface Interface9 extends Interface7{
sex:number
}
interface Interface10 extends Interface7,Interface8{
height:number
}
let json44:Interface9 = {
name:'1',
sex:1,
}
class Class1 {
private code:any
}
interface Interface11 extends Class1{
fn1(str:string):void
}
class Class2 extends Class1 implements Interface11 {
fn1(str: string): void {
}
}
泛型
function fun6<T,B,C>(
age:T,
label:B[],
value:Array<C>
):B[] {return label;}
let fun6output = fun6<string,number,boolean>('18',[1],[true]);
function identity<T>(arg: T): T {
return arg;
}
let myIdentity1: <T>(arg: T) => T = identity;
let myIdentity2: {<T>(arg: T): T} = identity;
interface IFX1{
<T>(code:T):T
}
let func71:IFX1 = identity;
interface IFX2<T>{
(code:T):T
}
let func72:IFX2<string> = identity;
class Cl1<T> {
setName(name:T):T{
return name;
}
}
let c1 = new Cl1<number>();
c1.setName(1);
interface IHasLength {
length: number;
}
function fun8<T extends IHasLength>(arg:T):T{
console.log(arg.length);
return arg;
}
fun8('a')
function getProperty<T, K extends keyof T>(obj: T, key: K): T[K] {
return obj[key]
}
let json7 = { a: 1, b: 2, c: 3, d: 4 };
getProperty(json7, "a");
类型
interface I1 {
name:string,
height:string,
}
interface I2 {
age:number,
height:string,
}
let test:I1 & I2 = {
name:'haha',
height:'height',
age:12,
};
let test1:string | number | boolean = '1';
interface IUnionCode1 {
name:string,
sex:string,
}
interface IUnionCode2 {
age:number,
sex:string,
}
let unionCode:IUnionCode1 | IUnionCode2 = {
name:'asadf',
age:1,
sex:'b'
};
console.log((<IUnionCode1>unionCode).name);
console.log((unionCode as IUnionCode1).name);
function isCode1(arg: IUnionCode1 | IUnionCode2): arg is IUnionCode1 {
return (<IUnionCode1>arg).name !== undefined;
}
if(isCode1(unionCode)){
console.log(unionCode.name);
} else {
console.log(unionCode.age);
}
interface I5 {
name:string | number
};
let test3:I5 = {
name:'123'
};
if(typeof test3.name === 'number'){
console.log(test3.name.toFixed(2));
}
class TClass1 {
}
let test4 = new TClass1();
if(test4 instanceof TClass1){
}
function test5(name:string | null):string{
if(name == null){
name = 'default';
}
return name;
}
function test6(name:string | null):string{
return name || 'default';
}
function test7(name:string | null):string{
console.log(name!.charAt(0));
console.log(name?.charAt(0));
return name || 'default';
}
type Itype1 = string;
type Itype2 = () => string;
type Itype3 = Itype1 | Itype2;
function test8(n: Itype3): Itype1 {
if (typeof n === 'string') {
return n;
}
else {
return n();
}
}
type Container<T> = { value: T };
type Tree<T> = {
value: T;
left: Tree<T>;
right: Tree<T>;
}
interface info {
name:name,
}
type name = 1 | 'code2' ;
function test9(name:info){
if(name.name == 1){
console.log(name);
} else if(name.name == 'code2'){
console.log(name);
} else {
console.log(name);
}
}
test9({
name:1
});
interface It1 {
name:'haha',
age:number
}
interface It2 {
name:'hehe',
width:number
}
interface It3 {
name:'hchc',
height:number
}
interface It4 {
name:'hbhb',
}
type name1 = It1 | It2 | It3 | It4;
function t1(params:name1):number{
switch (params.name) {
case "haha": return params.age * params.age;
case "hehe": return params.width * params.width;
case "hchc": return Math.PI * params.height * 2;
default:return 1;
}
}
t1({name:'hbhb'});