Development Study/Frontend

[TypeScript] TypeScript Grammar - 03~05

  • -
728x90

Previous Series

 

[TypeScript] TypeScript Grammar - 02

Previous Series [TypeScript] TypeScript Grammar - 01 1. Type Annotation TypeScript uses colon ( : ) behind variables to annotate a type const myMaxHamburger: number = 3; const answer: string = "Yes I can"; const drinks: boolean = true; You can code like va

time-map-installer.tistory.com


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

 

[TypeScript] TypeScript Grammar - 06~07

Previous Series [TypeScript] TypeScript Grammar - 03~05 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 wh

time-map-installer.tistory.com

 

728x90
Contents

포스팅 주소를 복사했습니다

이 글이 도움이 되었다면 공감 부탁드립니다.