Handbook Note : EveryDay Type
(一) Basic Type
1. Type Delcaration
: 包含bool、string、number
。
Delaration type not init
: 第一行。
Delaration type with init
: 第二行。
Delaration type with given value
: 第三行。
let num : number ;
let num : number = 9 ;
let num = 9 ;
2. Array Delcaration
:
Delaration type not init
: 第一行、第二行。
Delaration type with init
: 第三行。
let arr : number[];
let arr : Array<number>;
let arr = [1,2,3,4]
3. any
型別 : 變回JS
的使用。
- 可以執行任何行為 : 不會做
compile time
的檢查。
- 可以指定給任何的變數,也可能改變變數的
type
。
let variable : any = 1 ;
variable.toLowerCase();
let str : string = 1;
str.toLowerCase();
(二). Function
1. 預備知識 : function Decleration、function implement
。
int test(int num) {cout << num;}
int test(int);
int test(int num){cout <<num;}
2. TS 的 Function Decleration
:
- 需要設定
para
的type
:
- 可以編譯成功,但變回
JS
的模式。
- 沒有設定
type
的參數自行變成any type
。
- 不一定需要定義
return type
:
- 沒有定義下,
tsc
會自動依照回傳的資料定義。
- 有定義的形況下,會限制可以回傳的。若不相同,
tsc
會有error
function test():string{
return 1 ;
}
function test(){
return 1;
}
3. TS 的匿名function
- 有些匿名函式已經知道
type
,不用再指定arg type
(三). Type 和 interface
1. TS 的 Object Type : 指定Object需要的屬性。
type Account ={
name : string ,
memony : number,
}
let a : Account = {
name : "steven",
memony : 100
}
2. TS 的 Object interface : 指定Object需要的屬性。
interface Account {
name : string ,
memony : number,
}
let a : Account = {
name : "steven",
memony : 100
}
3. Object Type 和 Object interface 的不同
type Mytype ={
name : string
}
type Mytype ={
name : string ,
age : number
}
let arg01 : Mytype = {
name : "test"
}
interface Myinterface{
name : string
}
interface Myinterface{
name : string,
age : 10
}
let arg02 : Myinterface ={
name : "test",
age : 10 ,
}
interface father{
name : string
}
interface child extends father{
age : number
}
let obj : child ={
name : "steven",
age : 10
}
type father= {
name : string
}
type child = father & {
age : number
}
let obj : child ={
name : "steven",
age : 10
}
4. 都有duck type的性質 : 只要符合定義的都是一樣的型別
interface Mytype{
name : string
}
let template = {
name : "Steven"
age : 10
}
let obj : Mytype = template ;
interface Mytype{
name : string
}
let obj : Mytype = {
name : "steven",
age : 10
} ;
(四) . Optional Properity
1. 功用 : Object的屬性和function的參數可以Optional。
Object
屬性的optional
: 定義時可以不一定需要。
function
參數的optional
: 傳入時可以不一定需要。
2. 使用注意 :使用? operator
- 函示使用時 : 代表『如果存在下執行』。
- 但若不是存取必須
type-bind
的行為,ts不會檔(例如只有log出來)。
function test(arg?: string){
console.log(arg)
console.log(arg?.toLowerCase());
}
test();
test("LL");
- 物件使用時 : 也代表『如果存在下執行』。
- 但若不是存取必須
type-bind
的行為,ts不會檔(例如只有log出來)。
interface Test{
name? : string ,
age : number
}
let obj : Test = { name : "STEVEN" ,age: 10 }
console.log(obj.name?.toLowerCase());
console.log(obj.name);
(五) . Union Type
1. 功用 : 可以一次讓一個變數接受多種不同的型別。
- 可以對一般的變數宣告、函式參數、物件屬性使用。
- 不限可以union多少個。
let variable : number | string ;
function testFun(arg : number| string){ ...}
interface Test{
data : number | string
}
2. 使用注意 : narrowing
的使用,或是用as
。
narrowing
: 下一篇。
as
: 可以讓tsc過,但runtime可能有錯誤。
function testFun(arg : number| string){
console.log((arg as string).toLowerCase())
}