TypeScript Basics
47705 ワード
1.Primitive and Object
{
/**
* JavaScript
* Primitive: number, string, boolean, bigint, symbol, null, undefined
* Object: function, array.....
*/
// number
const num: number = -6;
// string
const str: string = 'hello';
// boolean
const boal: boolean = false;
// undefined
let name: undefined; // 💩
let age: number | undefined;
//보통 이런 union type 에 할당하는 용도, 단독으로는 잘쓰이지 않는다.
age = undefined;
age = 1;
function find(): number | undefined {
return undefined;
}
// null
let person: null; // 💩
let person2: string | null; //Union type에 많이 할당됨.
// unknown 💩 : 어떤 타입이라도 할당 가능
let notSure: unknown = 0;
notSure = 'he';
notSure = true;
// any 💩 : 어떤 타입이라도 할당 가능
let anything: any = 0;
anything = 'hello';
// void
function print(): void {
console.log('hello');
return;
}
let unusable: void = undefined; // 💩
// never : 함수가 영원히 끝에 도달하지 않는 것
function throwError(message: string): never {
// message -> server (log)
throw new Error(message);
while (true) {}
}
let neverEnding: never; // 💩
// objet
let obj: object; // 💩
function acceptSomeObject(obj: object) {}
acceptSomeObject({ name: 'ellie' });
acceptSomeObject({ animal: 'dog' });
}
2. Functions
{
// JavaScript 💩
function jsAdd(num1, num2) {
return num1 + num2;
}
// TypeScript ✨
function add(num1: number, num2: number): number {
return num1 + num2;
}
// JavaScript 💩
function jsFetchNum(id) {
return new Promise((resolve, reject) => {
resolve(100);
});
}
// TypeScript ✨
function fetchNum(id: string): Promise<number> {
return new Promise((resolve, reject) => {
resolve(100);
});
}
// JavaScript ✨ => TypeScript
// Optional parameter : 있 | 없
function printName(firstName: string, lastName?: string) {
console.log(firstName);
console.log(lastName); // undefined
}
printName('Steve', 'Jobs');
printName('Ellie');
printName('Anna');
// Default parameter
function printMessage(message: string = 'default message') {
console.log(message);
}
printMessage();
// Rest parameter : params 가 배열로 들어감
function addNumbers(...numbers: number[]): number {
return numbers.reduce((a, b) => a + b);
}
console.log(addNumbers(1, 2));
console.log(addNumbers(1, 2, 3, 4));
console.log(addNumbers(1, 2, 3, 4, 5, 0));
}
3. Array and Alias
{
// Array
//1번
const fruits: string[] = ['🍅', '🍌'];
//2번 (generic)
const scroes: Array<number> = [1, 3, 4];
//read only 의 경우 값을 변경할 수 없음 ex. push 불가능
function printArray(fruits: readonly string[]) {}
// Tuple:권장 x, android 언어와 같이 tuple로 형이 고정된 특수 케이스 제외하고는 -> interface, type alias, class
let student: [string, number];
student = ['name', 123];
student[0]; // name
student[1]; // 123
const [name, age] = student;
}
{
/**
* Type Aliases
*/
type Text = string;
const name: Text = 'ellie';
const address: Text = 'korea';
type Num = number;
type Student = {
name: string;
age: number;
};
const student: Student = {
name: 'ellie',
age: 12,
};
/**
* String Literal Types
*/
type Name = 'name';
let ellieName: Name;
ellieName = 'name';
type JSON = 'json';
const json: JSON = 'json';
type Boal = true;
}
4. Union and Discriminated
{
/**
* Union Types: OR
*/
type Direction = 'left' | 'right' | 'up' | 'down';
function move(direction: Direction) {
console.log(direction);
}
move('down');
type TileSize = 8 | 16 | 32;
const tile: TileSize = 16;
// function: login -> success, fail ⏱
type SuccessState = {
response: {
body: string;
};
};
type FailState = {
reason: string;
};
type LoginState = SuccessState | FailState;
// 변수값에 union 타입 할당
function login(): LoginState {
return {
response: {
body: 'logged in!',
},
};
}
// printLoginState(state: LoginState)
// success -> 🎉 body
// fail -> 😭 reason
function printLoginState(state: LoginState) {
if ('response' in state) {
console.log(`🎉 ${state.response.body}`);
} else {
console.log(`😭 ${state.reason}`);
}
}
}
{
// function: login -> success, fail ⏱
type SuccessState = {
result: 'success'; // 똑같은 key 값을 할당 ('in' 대신 사용하는 방법)
response: {
body: string;
};
};
type FailState = {
result: 'fail';
reason: string;
};
type LoginState = SuccessState | FailState;
function login(): LoginState {
return {
result: 'success',
response: {
body: 'logged in!',
},
};
}
// printLoginState(state: LoginState)
// success -> 🎉 body
// fail -> 😭 reason
function printLoginState(state: LoginState) {
if (state.result === 'success') {
console.log(`🎉 ${state.response.body}`);
} else {
console.log(`😭 ${state.reason}`);
}
}
}
5.Intersection and Enum
{
/**
* Intersection Types: &
*/
type Student = {
name: string;
score: number;
};
type Worker = {
empolyeeId: number;
work: () => void;
};
function internWork(person: Student & Worker) {
//& 연산자를 사용하면 두개 type의 key 값을 모두 가진다.
console.log(person.name, person.empolyeeId, person.work());
}
internWork({
name: 'ellie',
score: 1,
empolyeeId: 123,
work: () => {},
});
}
{
/**
* Enum
*/
// JavaScript (보통 모두 대문자로 표기)
const MAX_NUM = 6;
const MAX_STUDENTS_PER_CLASS = 10;
const MONDAY = 0;
const TUESDAY = 1;
const WEDNESDAY = 2;
const DAYS_ENUM = Object.freeze({ MONDAY: 0, TUESDAY: 1, WEDNESDAY: 2 });// freeze를 사용하면 변형이 불가능해짐
const dayOfToday = DAYS_ENUM.MONDAY;
// TypeScript
type DaysOfWeek = 'Monday' | 'Tuesday' | 'Wednesday';
//enum 을 사용하면 자동으로 value 값으로 index 할당
enum Days {
Monday,
Tuesday,
Wednesday,
Thursday,
Friday,
Saturday,
Sunday,
}
console.log(Days.Monday);
let day: Days = Days.Saturday;
day = Days.Tuesday;
day = 10;
console.log(day);
let dayOfweek: DaysOfWeek = 'Monday';
dayOfweek = 'Wednesday';
}
6. Interface
/**
* Type Inference
*/
let text = 'hello';
function print(message = 'hello') {
console.log(message);
}
print('hello');
function add(x: number, y: number): number {
return x + y;
}
const result = add(1, 2);
7. Assertion
{
/**
* Type Assertions 💩 왠만하면 쓰지말것!
*/
function jsStrFunc(): any {
return 2;
}
const result = jsStrFunc();
console.log((result as string).length);
//result가 string 이라는 확신이 있을 때 사용.
console.log((<string>result).length);
const wrong: any = 5;
console.log((wrong as Array<number>).push(1)); // 😱
function findNumbers(): number[] | undefined {
return undefined;
}
const numbers = findNumbers()!;
numbers.push(2); // 😱
const button = document.querySelector('class')!;
}
Reference
この問題について(TypeScript Basics), 我々は、より多くの情報をここで見つけました https://velog.io/@leejyart/TS-1.-Basicsテキストは自由に共有またはコピーできます。ただし、このドキュメントのURLは参考URLとして残しておいてください。
Collection and Share based on the CC Protocol