728x90
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: <type>
It is essential to write this annotation, so be careful to pass by this
1_01. Type Declaration_string
let str: string;
let red: string = 'Red';
let green: string = 'Green';
let myColor: string = `Do you like Color ${green}?`;
You can code like let <variable name>: <type>
String type is just a letter that can't be calculated
1_02. Type Declaration_number
let num: number;
let integer: number = 6;
let float: number = 3.14;
let hex: number = NaN;
In Java, you can't use integers as float. But in Typescript, we can use them equally
1_03. Type Declaration_union type
let name: string | number = 'john';
let name: string | number = 123;
let name: string[] | number = ['test1', 'test2', 'test3'];
You can grammarly understand this as "You can use either A or B"
For example, we can call the name either string or number.
Therefore, it can be more flexible
1_04. Type Declaration_boolean
let isBool: boolean;
let isDone: boolean = false;
const size: number = 101;
const isHuge: boolean = size >= 99; // <- this area return as boolean type
End
Next Series
728x90
'Development Study > Frontend' 카테고리의 다른 글
[TypeScript] TypeScript Grammar - 03~05 (0) | 2023.01.07 |
---|---|
[TypeScript] TypeScript Grammar - 02 (0) | 2023.01.07 |
[TypeScript] Differences between .ts & .tsx (0) | 2023.01.07 |
[배경지식] CSS 프레임워크를 알아보자 (1) | 2022.12.31 |
[배경지식] JavaScript? TypeScript? 이 둘의 차이는 무엇일까? (0) | 2022.12.31 |