JavaScript : dynamic type
TypeScript : static type
{ property: type }
const person: {
name: string;
age: number;
} = {
name: "Max",
age: 30,
};
console.log(person.name);
{ property : { property : type } }
const product : {
id: string;
price: number;
details: {
title: string;
description: string;
}
} = {
id: 'abc1',
price: 12.99,
details: {
title: 'Red Carpet',
description: 'A great carpet - almost brand-new!'
}
}
type[]
const myFavoriteHobbies : string[] = ['sport', 'movie'];
const myFavoriteHobbies : string[] = ['sport', 5];
// Error : 'number' 형식은 'string' 형식에 할당될 수 없음
any[]
를 사용const myFavoriteHobbies : any[] = ['sport', 5];
let role : [number, string] = [1, 'author'];
role = [0, 'admin'];
role = [0, 1];
// Error : 'number' 형식은 'string' 형식에 할당될 수 없음
const role : [number, string] = [1, 'author'];
role[1] = 'admin'; // role = [1, 'admin']
role[1] = 0;
// Error : 'number' 형식은 'string' 형식에 할당될 수 없음
const role : [number, string] = [1, 'author'];
role.push('admin') // role = [1, 'autho', 'admin']
enum name { member1, member2, ... };
enum Role { ADMIN = 1, AUTHOR, READ_ONLY };
const person: {
name: string;
age: number;
hobbies: string[];
role: number;
} = {
name: 'Max',
age: 30,
hobbies: ['sports', 'cooking'],
role: Role.ADMIN,
};
console.log(Role[2]) // AUTHOR
enum Direction {
Up = "UP",
Down = "DOWN",
Left = "LEFT",
Right = "RIGHT",
}
enum BooleanLikeHeterogeneousEnum {
No = 0,
Yes = "YES",
}
let obj: any = { x: 0 };
obj.foo();
obj();
obj.bar = 100;
obj = "hello";
const n: number = obj;
// 모든 코드에서 error가 발생하지 않음
string | number
)function printId(id: number | string) {
console.log("Your ID is: " + id);
}
printId(101);
printId("202");
printId({ myID: 22342 }); // Error 발생
function printId(id: number | string) {
console.log(id.toUpperCase());
// Error 발생, id가 number일 때는 toUpperCase 메소드 사용 X
}
function combine(input1, input2) {
var result;
if (typeof input1 === 'number' && typeof input2 === 'number') {
result = input1 + input1;
}
else {
result = input1.toString() + input2.toString();
}
return result;
}
var combineAge = combine(20, 35);
console.log(combineAge); // 55
var combineName = combine('Max', 'Anna');
console.log(combineName); //MaxAnna
function getFirstThree(x: number[] | string) {
return x.slice(0, 3);
// array와 string 둘 다 slice 메소드를 갖고 있으므로 괜찮음
}
const constantString = "Hello World";
constantString; // const constantString: "Hello World"
stringA | stringB
)function combine(
input1: number | string,
input2: number | string,
resultConversion: 'as-number' | 'as-text',
) {
let result;
if (
(typeof input1 === 'number' && typeof input2 === 'number') ||
resultConversion === 'as-number'
) {
result = +input1 + +input2;
} else {
result = input1.toString() + input2.toString();
}
return result;
}
const combineAge = combine(20, 35, 'as-number');
console.log(combineAge); // 55
const combineStringAge = combine('20', '35', 'as-number');
console.log(combineStringAge); // 55
const combineName = combine('Max', 'Anna', 'as-text');
console.log(combineName); // MaxAnna
type CustomAlias
type Point = {
x: number;
y: number;
};
function printCoord(pt: Point) {
console.log("The coordinate's x value is " + pt.x);
console.log("The coordinate's y value is " + pt.y);
}
printCoord({ x: 100, y: 100 });
type Combinable = number | string;
type ConversionDescriptor = 'as-number' | 'as-text';
function combine(
input1: Combinable,
input2: Combinable,
resultConversion: ConversionDescriptor,
) {
let result;
if (
(typeof input1 === 'number' && typeof input2 === 'number') ||
resultConversion === 'as-number'
) {
result = +input1 + +input2;
} else {
result = input1.toString() + input2.toString();
}
return result;
}
return type : 함수의 반환값 타입 (예: function foo(): number
)
function foo(): void
)function type : 함수의 매개변수와 반환값을 나타내는 타입 (예: const add : (n1:number, n2:number) => number;
)
callback func type: 콜백함수의 타입을 선언함
function addAndHandle(n1: number, n2: nnumber, cb: (num: number) => void) {
const result = n1 + n2;
cb(result)
}
// cb의 반환값 타입 void = ddAndHandle 함수에서 cb 함수의 반환값을 사용하지 않는 다는 것을 의미
addAndHandle(1, 2, (result) => { console.log(result)});