Primitive Type
기본 타입입니다 간단함
let 나이: number = 30;
let 이름: string = '엄준식';
let 여친여부: boolean = false;
Structural Type
자료구조, 함수등을 나타내는 타입입니다
interface UserType {
name: string;
age: number;
}
const user1: UserType = {
name: "엄준식",
age: 30,
};
const users: UserType[] = [
{
name: "엄준식",
age: 30,
},
{
name: "엄준석",
age: 31,
},
];
type Toss = 'toss';
type TossPayments = `${Toss} payments`;
type Toss = 'toss';
type Companies = 'core' | 'bank' | 'securities' | 'payments';
type TossCompanies = `${Toss} ${Companies}`
let string_arr: string[] = ["엄", "준", "식"];
let number_arr: number[] = [1, 2, 3];
let mix_arr: (number | string)[] = ["엄", 1, "준"];
let person: [number, string] = [30, "엄준식"];
let persons: [number, string][] = [
[31, "엄준식1"],
[32, "엄준식2"],
[33, "엄준식3"],
[34, "엄준식4"],
[35, "엄준식5"],
[36, "엄준식6"],
];
enum Direction {
Up = 1,
Down,
Left,
Right,
}
enum Direction {
Up,
Down,
Left,
Right,
}
enum Example {
A = getUserInfo(),
B,
}
여기서 잠깐
Any는 죄악입니다
let pig: any = "돼지";
pig = 1;
pig = false;
let age: number | string;
age = 10;
age = '15';
type Color = "Red" | "Black" | "White";
let color: Color;
color = "Red";
const myAdd = (x: number, y: number): number => {
return x + y;
};
const myAdd2: (baseValue: number, increment: number) => number = (x, y) => {
return x + y;
};
const myName = (firstName: string, lastName?: string): string => {
if (lastName) return firstName + " " + lastName;
return firstName;
};
const myName2: (firstName: string, lastName?: string) => string = (
firstName,
lastName
) => {
if (lastName) return firstName + " " + lastName;
else return firstName;
};
const printN: (n: string) => void = (n) => {
console.log(n);
};
const printN2 = (n: string): void => {
console.log(n);
};
function fn(input: never) { return input }
declare let myNever: never
fn(myNever)
fn()
fn(1)
fn('foo')
declare let myAny: any
fn(myAny)