https://bit.ly/2XuVqBJ (這篇必看,不分享對不起自己)
//原來南無觀世音菩薩可以這麼用 真的笑鼠我 今天來介紹object~
上一篇紀錄了原始資料型別(Primitive Types),這一篇要來探險物件型別(Object types)咯~ 會介紹到的型別包括:object(物件) 、 arrays(陣列) 、function(函式)。
像一般在 javascript 宣告物件應用在 TypeScript :
let person = {
name: "iris",
age: 18
};
在 TypeScript 中會按照物件本身自動推論出資料型別(Type Inference)。
來修改看看物件會出現什麼狀況:
person.name = "aka 廢廢前端" //ok 型別相同
person = {
name: "aa", //ok 覆寫的物件格式必須完全相同
age: 20
}
person.name = false; // error: Type 'boolean' is not assignable to type 'string'.
person.job = "在家躺" //error: Property 'job' does not exist on type
person = {
name: "bb" //error: Property 'age' is missing in type
}
person = {
gender : "male", //error: Type '{ gender: string; job: string; }' is not assignable to type '{ name: string; age: number; }'
job : "在家躺"
}
delete person.name; //可執行刪除屬性
//tsconfig 開啟嚴謹模式 會報錯提醒
delete person.name;//error: The operand of a 'delete' operator must be optional.
結論:
除了 TypeScript 會自動推論物件型別外,我們也可以自己去定義物件變數:
let person2: object = {
name: "iris",
age: 18
};
他跟一般 JSON 物件格式有什麼差別呢?
person2.name = "aka 廢廢前端" //error: Property 'name' does not exist on type 'object'.
person2 = {
name: "aa", //ok
age: 20
}
person2.name = false; // error: Property 'name' does not exist on type 'object'.
person2.job = "在家躺" //error: Property 'job' does not exist on type 'object'.
person2 = {
name: "bb" //ok
}
person2 = {
gender : "male", //ok
job : "在家躺",
}
delete person2.name; //error: Property 'name' does not exist on type 'object'.
驚不驚喜,意不意外~我也沒想到,居然連值型別相同都不能覆寫。所以在定義object時只定義了 person2 為空object {}
, 卻沒有細節屬性可以用 QQ
結論:
雖然 TS 已經幫我們自動推論出型別,但也可以手動定義型別。感覺使用方法1,一般 JSON 物件格式,更方便快速吧,符合廢廢前端的特質 XD
let person3 : {name: string, age: number} = {
name: "iris",
age: 18
}
在function中定義參數型別,person4: { name: string, age: number }
,官網說也可使用分號去分隔參數如:person4: { name: string; age: number }
。
const getUserInfo = (person4: { name: string, age: number }) =>{
console.log(` Hello, my name is ${person4.name}. I'm ${person4.age} years old.`);
}
getUserInfo({ name: "iris", age: 18 });
我們可以去指定部分或全部屬性為可選屬性,方法是在屬性名稱後面加上一個?
。如下面例子,age
改為可選屬性,可輸入或不輸入。
const getUserInfo2 = (person5: { name: string, age?: number }) =>{
console.log(` Hello, my name is ${person5.name}. I'm ${person5.age} years old.`);
}
getUserInfo2({ name: "iris"}); //Hello, my name is iris. I'm undefined years old.
以上例子沒帶age
參數雖然沒報錯,但函式內有用到age
時他會回傳 undefined
, 這樣也不是我們要的,我們在去針對undefined
做判斷即可。
const getUserInfo2 = (person5: { name: string, age?: number }) =>{
if(person5.age !== undefined){
console.log(`Hello, my name is ${person5.name}. I'm ${person5.age} years old.`);
}else{
console.log(`Hello, my name is ${person5.name}.`);
}
}
getUserInfo2({ name: "iris"}); //Hello, my name is iris.
getUserInfo2({ name: "iris", age: 18 }); //Hello, my name is iris. I'm 18 years old.
嗚嗚 終於寫完 object 了,原本預計本篇是要寫 object,array 及 function , 但小編我已累,而且讀起來會太長,就下篇再寫 array 及 function了。耶!來去休息一下~
https://ithelp.ithome.com.tw/articles/10214840#h5o-8
https://www.typescriptlang.org/docs/handbook/2/everyday-types.html#object-types