728x90
// 타입 종류
// 문자
let str: string;
let red: string = "Red";
let green: string = "Green";
let myColot: string = `My color is ${red}`;
let yourColor: string = "Your color is" + green;
// 숫자
let num: number;
let integer: number = 6;
let float: number = 3.14;
let infinity: number = Infinity;
let nan: number = NaN;
// 불린
let isBoolean: boolean;
let isDone: boolean = false;
// Null / Undefined
let nul: null;
let und: undefined;
//console.log(null) -> 오류. null변수를 추가해줘야함.
console.log(und); //undefined출력
// 배열
const fruits: string[] = ["Apple", "Banana", "Cherry"];
const numbers: number[] = [1, 2, 3, 4, 5, 6, 7];
const union: (string | number)[] = ["Apple", 1, 2, "Banana", 3];
// 객체
const obj: object = {};
const arr: object = [];
const func: object = function () {};
const userA: {
name: string;
age: number;
isValid: boolean;
} = {
name: "Doocong",
age: 22,
isValid: true,
};
//+interface 사용!
// 함수
const add: (x: number, y: number) => number = function (x, y) {
return x + y;
};
// const a: number = add(1, 2);
const hello1: () => void = function () {
console.log("Hello world!");
};
const h: void = hello1();
// Any
let hello: any = "Hello world";
hello = 123;
hello = false;
hello = null;
hello = {};
hello = [];
hello = function () {};
// Unknown
const a: any = 123;
const u: unknown = 123;
const any: any = a;
const boo: boolean = a;
const num: number = a;
const arr: string[] = a;
const obj: { x: string; y: number } = a;
// Tuple
const Tuple: [string, number, boolean] = ["a", 1, false];
const users: [number, string, boolean][] = [
[1, "Neo", true],
[2, "Evan", false],
[3, "Lewis", true],
];
// Void -> return이 없을 때 사용.
function hello(msg: string): void {
console.log(`Hello ${msg}`);
}
const hi: void = hello("world");
// Never
// const nev: [] = [];
// nev.push(3);
// Union
let union: string | number;
union = "HEllo";
union = 123;
// union = false -> 오류
// Intersection
interface User {
name: string;
age: number;
}
interface Validation {
isValid: boolean;
}
const doocong: User & Validation = {
name: "doocong",
age: 22,
isValid: true,
};
728x90
'TS' 카테고리의 다른 글
인터페이스(Interface) (0) | 2024.02.25 |
---|---|
TS 기초 개요 (0) | 2024.02.21 |