TypeScript
A javaScript Superset
A Language building up on javaScript
Adds new Features + Advantages to javascript (μλ‘μ΄ κ²μ΄ μλλ€)
Browser can't execute it
Compiled to js
const add = (num1, num2) => num1 + num2;
console.log(add('1','2'));
add('1','2')
Unwanted Behavior at Runtime
result 23
Mitigation Stategies
Add if check to add fuction Validate & sanitize user input
const button = document.querySelector("button");
const input1 = document.querySelector("num1");
const input2 = document.querySelector("num2");
const add = (num1, num2) => num1 + num2;
button.addEventListener('click', () => consle.log(input1.value, input2.value));
this is not a technical error
it's not an error which is thrown but it's a logical error
the value of an input element is always a number
const add = (num1, num2) => {
if(typeof num1 === "number" && typeof num2 === "number") {
return num1 + num2
}
return Number(num1) + Number(num2);
}
using typescript
const button = document.querySelector("button");
const input1 = document.querySelector("num1")! as HTMLInputElement;
const input2 = document.querySelector("num2");
const add = (num1: number, num2: number) => num1 + num2;
button.addEventListener('click', () => consle.log(input1.value, input2.value));
tsc using-ts.ts
compiler error
μ¬κΈ°μ !
λ μ λ nullμ΄ μ€μ§ μλλ€λ λ»μ΄λ€.
νμ μ€ν¬λ¦½νΈλ₯Ό μΆκ°νλκ²μ typeκ³Ό dataκ° λ§€μ° μ€μνλ€.
μ₯μ
clean code
μ€λ₯ λ°©μ§(μκΈ°μΉ μμ μν©μ νΌν μ μλ€.)
μ΄λ»κ² μλνλμ§ λͺ μμ μΌλ‘ μ§μ
μ»΄νμΌνμ¬ μ΄μ λ²μ μ λΈλΌμ°μ λ μ§μνλ€.(Next-gen JavaScript)
Non-javaScript Features like Interfaces or Generics
Meta-Programming Features like Decorators
Rich Configuration Options
Modern Tooling that helps even in non-TypeScript Projects
lite server
npm install --save-dev lite-server
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1",
"start": "lite-server"
}
Core Types
number
String
Boolean
Object
Array
Tuple
Enum
Any
number
function add(n1: number, n2: number) {
return n1 + n2;
}
const number1 = '5';
const number2 = 2.8;
const result = add(number1, number2);
console.log(result);
typeScript's type system only helps you during development.
λΈλΌμ°μ λ μ§μν΄μ£Όμ§ μλλ€.
JavaScript uses "dynamic types" (resolved at runtime), TypeScript uses "static types" (set during development).
Object Types
const person = {
name: 'sla'
};
console.log(person.nickname);
nicknameμ μ€μ νμ§ μμ μνμμ nicknameμ νμΈνκ³ μ νλ€λ©΄ κ°λ°νκ²½μμ νμ μ€ν¬λ¦½νΈκ° μ€λ₯λ₯Ό μλ €μ€λ€.
Property 'nickname' does not exist on type '{ name: string; age: number; }'.
6 console.log(person.nickname);
const person: {
name: string;
} = {
name: 'sla'
};
console.log(person.name);
Array
const person = {
name: 'sla',
hobbies: ['Sports', 'Cooking']
};
for (const hobby of person.hobbies) {
console.log(hobby.toLocaleUpperCase());
}
console.log(person.name);
toLocaleUpperCaseλ₯Ό μΌμλλ λ¬Έμ κ° μλ€.
for (const hobby of person.hobbies) {
console.log(hobby.map());
}
mapμ Array λ©μλκΈ° λλ¬Έμ νμ μ€ν¬λ¦½νΈκ° μλ¬λ₯Ό λΏλΏν΄μ€λ€.
Tuple
const person: {
name: string;
age: number;
hobbies: string[];
role: [number, string];
} = {
name: 'sla',
hobbies: ['Sports', 'Cooking'],
role: [2, 'author']
};
Enum
const ADMIN = 0;
const person = {
name: 'sla',
hobbies: ['Sports', 'Cooking'],
role: ADMIN
};
jsμμ μμκ΄λ¦¬λ₯Ό ν λλ constμ λλ¬Έμ λ³μλͺ μ μ΄μ©νμ¬ νμλ€.
enumμ μ΄κ±°νμΌλ‘ μ½κ² ν μ μλ€.
typescriptλ‘ λ°κΎΌλ€λ©΄
enum Role { ADMIN, READ_ONLY, AUTHOR };
const person = {
name: 'sla',
hobbies: ['Sports', 'Cooking'],
role: Role.ADMIN
};
enum Role { ADMIN, READ_ONLY, AUTHOR };
μ΄λ κ² μ¬μ©ν μ μλ€. λ°°μ΄μ²λΌ 0, 1, 2μ ν΄λΉλλ€.
λ³νν νμΌμμ νμΈν μ μλ€.
var Role;
(function (Role) {
Role[Role["ADMIN"] = 0] = "ADMIN";
Role[Role["READ_ONLY"] = 1] = "READ_ONLY";
Role[Role["AUTHOR"] = 2] = "AUTHOR";
})(Role || (Role = {}));
enum Role { ADMIN = 5, READ_ONLY = 'READ_ONLY', AUTHOR };
μλ₯Ό λ³κ²½ν μ μλ μ«μλ λκ³ ν μ€νΈλ λλ€.
any
μ΅λν μ°λ κ²μ νΌνμ
λͺ μμ μΌλ‘ νμ μ λνλ΄μ§ μκ³ μ μΆλ₯Ό ν΅νλ€λ©΄ νμ μ€ν¬λ¦½νΈ μ₯μ x
Union Types
function combine(input1: number | string, input2: number | string) {
let result;
if (typeof input1 === 'number' && typeof input2 === 'number') {
result = input1 + input2;
} else {
result = input1.toString() + input2.toString();
}
return result;
}
const combinedAges = combine(30, 26);
console.log(combinedAges)
|
μ μ΄μ©νμ¬ νμ
μ μ§μ ν μ μλ€.
Type Aliases
type Combinable = number | string;
function combine(input1: Combinable, input2: Combinable) {
// ...code
}
typeμ μ΄μ©νμ¬ μλ¨μ μ§μ ν μ μλ€.
Function Return Types and Void
function add(n1: number, n2: number): number {
return n1 + n2
}
parameter λ§€κ°λ³μ μμ :
λ₯Ό μ΄μ©νμ¬ returnκ°μ νμ
μ μ μν΄μ€λ€.
function printResult(num: number): void {
console.log('Result: ' + num);
}
returnκ°μ΄ μμ λλ, return;λ§ ν λ voidλ‘ μ μνλ€.
undefined μΈ κ²½μ°μ μλ κΉ?
function printResult(num: number): undefiend {
console.log('Result: ' + num);
return;
}
return;
μ΄λ° μν©μ λ§ν μ μλ€. νμ§λ§ void
λ‘ μ΄λ€.
Function Types
function add(n1: number, n2: number): number {
return n1 + n2
}
let combineValues: (a: number, b: number) => number;
combineValues = add;
console.log(combineValues(8, 8));
λ³μμ νμ
μ ν¨μλ‘ μ§μ ν λ functionμ΄λΌκ³ νμ§ μλλ€. (a: number, b: number) => number;
μΌλ‘ μ§μ νλ€.
Function Types and Callbacks
function add(n1: number, n2: number, cb: (num: number) => void): number {
cb(result);
return n1 + n2
}
console.log(add(1, 2, (num) => console.log(num)));
μΈμμ λν μ μλ λ§€κ°λ³μμμ νλ€. μμ¬μ΄ λ€μ΄ νμΈνλ λ°©λ²μ addν¨μλ₯Ό νΈμΆ ν λ λ§μ°μ€λ₯Ό μ¬λ €λ³΄λ©΄ νμ μ΄ μ§μ λμ΄ μλ€.
The Unknown Type
let userInput: unknown;
let useName: string;
userInput = 5;
userInput = 'sla';
userName = userInput;
μ΄λ κ² νλ©΄ λ°νμ μλ¬κ° λλ€. unknownλ‘ μ€μ ν건 stringμ ν λΉν μ μλ€.
κ·Έλ λ€λ©΄ anyλ?
let userInput: any;
let userName: string;
userInput = 5;
userInput = 'sla';
userName = userInput;
λ¬Έμ μλ€. anyλ‘ μ€μ νλ κ²μ μλ°μ€ν¬λ¦½νΈμ²λΌ μ μ°νκ² μ°κ² λ€λ λ»μ΄κ³ κΈ°λ³Έμ μΌλ‘ νμ
κ²μ¬λ₯Ό λΉνμ±ννκ³ νμ
μ€ν¬λ¦½νΈλ₯Ό μ°λ κ²μ ν¬κΈ°νκ² λ€
λ λ»μ΄λ€.
userInputμ ν λΉνλ λ²
let userInput: unknown;
let userName: string;
userInput = 5;
userInput = 'sla';
if (typeof userInput === 'string') {
userName = userInput;
}
extra checkλ₯Ό μΆκ°νλ€.
The Never Type
function generateError(message: string, code: number): never {
throw {message: message, errorCode: code};
// while (true) {}
}
generateError('An error occurred!', 500);
ν¨μμμ μ λ returnμ΄ μμ λ μ¬μ©νλ€.
generateErrorμ λ§μ°μ€λ₯Ό μ¬λ¦¬λ©΄ voidκ° λ¨μ§λ§ ν¨μ¬ λͺ ννλ€.
Last updated
Was this helpful?