Development Study/Frontend

[TypeScript] TypeScript Grammar - 02

  • -
728x90

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 variable: It is essential to write this annotation, so

time-map-installer.tistory.com


2.  Array

In this section, we are going to find out what's typescript's array types are


2_01.  Array_number

let numArray1: number[] = [1, 2, 3, 4, 5, 7, 12, 13];
let numArray2: Array<number> = [1, 2, 3]

2_02.  Array_string

let apple: string[] = ['apple', 'macbook', 'iphone']
let apple2: Array<string> = ['apple logo', 'macbook air', 'iphone X']

In Java, you can't use integers as float. But in Typescript, we can use them equally


2_03.  Array_multiple

let arr: (string | number)[] = ['Apple', 1, 2, 'Strawberry', 5]
let arr1: Array<string | number> = ['Apple', 1, 2, 'Strawberry', 5]

 

You can grammarly understand this as "You can fill this list with A and B"

For example, we can call the list filled with strings and numbers.

Therefore, it can be more flexible


2_04.  Array_any

 

let whatArr: any[] = [1, 3, {}, [], 'hello', true]

Letterly any type can be used in this case


End

 

 

Next 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 when we have to define two or more variables 3_01. Type al

time-map-installer.tistory.com

 

728x90
Contents

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

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