티스토리 뷰

타입스크립트 1~2일차

알게 된 점

TypeScript는 Javascript의 superset(=자바스크립트 + 알파)이다.

  • 자바스크립트와 100% 호환이 되며, 이 외에 클래스,인터페이스 등 객체지향 프로그래밍 패턴을 제공한다.

명령어

  • npm ls -g ⇒ 글로벌 모듈 확인
  • npm i typescript -g ⇒ 타입 스크립트 사용하기 위하여 설치
  • tsc ⇒ 명령어 보기
  • tsc —version ⇒ 설치된 타입스크립트 버전 보기
  • tsc —init ⇒ tsconfig.json 생성(자동 셋팅)

마이크로소프트의 tsconfig 추천 세팅

 

GitHub - microsoft/TypeScript: TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

TypeScript is a superset of JavaScript that compiles to clean JavaScript output. - GitHub - microsoft/TypeScript: TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

github.com

 

기초 문법

// Boolean
let isDone: boolean = false;

// Number
let decimal: number = 6;
let hex: number = 0xf00d;
let binary: number = 0b1010;
let octal: number = 0o744;

// String
let color: string = "blue";
color = "red";

// Template Literal
let fullName: string = `Bob Bobbington`;
let age: number = 37;
let sentence: string = `Hello, my name is ${fullName}.
I'll be ${age + 1} years old next month.`;

// Array
let list1: number[] = [1, 2, 3];
let list2: Array<string> = ["안", "녕"];

// Tuple
let x: [string, number]; // 타입과 개수가 고정된 배열, 요소들의 타입이 다를 수 있음
x = ["hello", 10]; // if 타입이 일치하지 않거나 정해지지 않은 인덱스에 있는 요소에 접근하면 오류

// Enum
enum Color {
  Red,
  Green,
  Blue,
}
let c: Color = Color.Green; // c = 1

enum Color2 {Red = 1, Green, Blue} // 시작값을 1로 설정
let c2: Color2 = Color2.Green;

enum Color3 {Red = 1, Green, Blue}
let colorName: string = Color3[2]; // colorName = 'Green'

// Any - 쓰지 않는 것이 좋음(Typescript를 쓰는 이유가 없음)
let notSure: any = 4;
notSure = "maybe a string instead";
notSure = false;

// Void - 보통 함수에서 반환 값이 없을 때 반환 타입을 표현하기 위해 쓰임
function warnUser(): void {
    console.log("This is my warning message");
}

// Null, Undefined
let u: undefined = undefined;
let n: null = null;

// Never -  절대 발생할 수 없는 타입, 함수 표현식이나 화살표 함수 표현식에서 항상 오류를 발생시키거나 절대 반환하지 않는 반환 타입
function error(message: string): never {
  throw new Error(message);
}

// Object - 원시 타입이 아닌 타입
function create(o: object | null): void {}

create({ prop: 0 }); // 성공
create(null); // 성공
// create(42); // 오류
// create("string"); // 오류

// Type assertions
let someValue: any = "this is a string";
let strLength: number = (<string>someValue).length; // angle-bracket 문법

let someValue2: any = "this is a string";
let strLength2: number = (someValue2 as string).length; // as 문법

 

선발대(숙제) - 이중화 기능 구현 아이디어

단순히 서버를 2개 이용하여 Master와 Standby로 사용한다..?

db 이중화 or 서버 이중화

일정 주기 or 실시간으로 Standby를 Active와 동기화

전부 다 동기화하는 것은 너무 무거우니까 로그만 남겨두는 방식으로?

 

댓글
공지사항
최근에 올라온 글
최근에 달린 댓글
Total
Today
Yesterday
링크
«   2024/09   »
1 2 3 4 5 6 7
8 9 10 11 12 13 14
15 16 17 18 19 20 21
22 23 24 25 26 27 28
29 30
글 보관함