728x90
Previous Series
3. Type alias
This Posting contains how to alias type to other variables
type myType = number | string;
let grade: myType = 3
Use type to create type and use to annotate another.
Usually, we use it when we have to define two or more variables
3_01. Type alias_array tuple
type Fruit = [string, boolean];
let apple: Fruit = ['apple', true]
let apple: Fruit = [1000, false] // error
3_02. Type alias_designate all types
type Fruit = {
name: string
}
let apple: Fruit = {name: 'green apple'}
let apple: Fruit = {name: 1000} // error
When we use with forEach:
const fru_data: string[] = ['red_fru', 'pur_fru', 'yel_fru'];
interface FruData{
red_fru : number;
comic : number;
romance : number;
}
/* or you can use FruData like this
interface FruData{
[key: string] : number;
}
*/
let data : FruData = {
'red_fru': 10,
'pur_fru': 7,
'yel_fru': 13
};
genre_data.forEach(fru_col => {
data[`${fru_col}`] += 1
});
// result
// red_fru = 11
// pur_fru = 8
// yel_fru = 14
4. Tuple
let member: [string, number, boolean] = ['Michael Jordan', 191, true]
/* results
member[0] => 'Michael Jordan'
member[1] => 191
member[2] => true
*/
5. Object
let names : {name: string} = {name: 'Steve'}
let names : {name: string} = {} // optional values
End
Next Series
728x90
'Development Study > Frontend' 카테고리의 다른 글
[TypeScript] TypeScript Grammar - 08 (0) | 2023.01.08 |
---|---|
[TypeScript] TypeScript Grammar - 06~07 (0) | 2023.01.08 |
[TypeScript] TypeScript Grammar - 02 (0) | 2023.01.07 |
[TypeScript] TypeScript Grammar - 01 (0) | 2023.01.07 |
[TypeScript] Differences between .ts & .tsx (0) | 2023.01.07 |