-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathts6.ts
More file actions
34 lines (27 loc) · 935 Bytes
/
ts6.ts
File metadata and controls
34 lines (27 loc) · 935 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
// جنریک ها
// میتوان برای کلاس و فامگشن و متد ها و اینترفیس ها تایپ های مختلفی به ازای هر نمونه ساخت
// class
class User<T, A>{
constructor(public name:T,public age:A){}
}
let U_1 = new User<string,number>('mohammad',19)
let U_2 = new User<string,string>('faeze',"18")
// function
function num<A,B>(num_1:A,num_2:B){
console.log(num_1 , num_2);
}
num<number,number>(1,15)
num<string,boolean>('faezeh',true)
// interface
interface person<one,two>{
name:one,
age:two
}
let boy:person<string,number> = {name:'mohammad',age:19};
let gerl:person<string,string> = {name:'mohammad',age:'18'};
// محدود کردن جنریک بین چند تایپ
function num_2<A extends number | string,B extends boolean | number>(num_1:A,num_2:B){
console.log(num_1 , num_2);
}
num_2<number,number>(1,15)
num_2<string,boolean>('faezeh',true)