01.Typescript - nkiateam/angular-tutorial GitHub Wiki
JavaScript that scales.
TypeScript๋ 2012๋
10์์ Microsoft์์ ๋ฐํํ์๋ค.
ES2015์ ๊ธฐ๋ฐ์ผ๋กํ OOPํํ์ ์ ์ ํ์ดํ ์ธ์ด์ด๋ฉฐ, JavaScript๋ก ์ปดํ์ผ ๋๋ค.
๋๊ท๋ชจ Application ๊ฐ๋ฐ์์ ์๋ฐ์คํฌ๋ฆฝํธ์ ๋์ ์์ ๋(๋์ ํ์ดํ)์ ์ํด์ ๋ฐ์๋๋ ์ค๋ฅ์ ๋ํ ํด๊ฒฐ์ ์ ์ฐพ๊ณ ์ ๊ฐ๋ฐํ๊ฒ
๋์๋ค.
๋์ฌ ๋น์์๋ Windows ์ด์ธ์ ๋ค๋ฅธ ๊ฐ๋ฐ ํ๊ฒฝ์ง์์ด ๋ฏธ๋นํด์ ๋ถ์ ์ ์ธ ์๊ฒฌ๋ ์ ์ง ์์๋ ๊ฒ์ผ๋ก ์๊ณ ์์ง๋ง,
์ํ๊ณ ์ง์์ด ์ ์ ํ์ฅ๋๊ณ , ์ต๊ทผ์๋ Angular ํ์์ ์ด ์ธ์ด๋ฅผ ๋ฉ์ธ ์ธ์ด๋ก ์ฑํํ๋ฉด์ ํ์ฌ๋ก์๋ ์ ์ ํ์ดํ์ ์ง์ํ๋ ๋ค๋ฅธ JavaScript ์ ์ฒ๋ฆฌ๊ธฐ(Flow ๋ฑ)์ ๋นํด์ ์ปค๋ฎค๋ํฐ๋ ์์ ์ฑ ์ธก๋ฉด์์ ์์๋๊ฐ๋ ๋ชจ์์๋ค.
๊ธฐ์กด IDE์์ ์๋ฐ์คํฌ๋ฆฝํธ๋ฅผ ์ํ ํ ์ง์ํ์ง ์๋ ๋ฌธ์ ์ ๋ ํด๊ฒฐํ๊ธฐ ํ๊ณ ์ ํ์ฌ๋ Visual Studio, Sublime Text2๋ฑ์ IDE์์
์ง์ํ๊ณ ์๋ค. ๋ฟ๋ง ์๋๋ผ ๋ชจ๋ ๋ธ๋ผ์ฐ์ , ๋ชจ๋ ํธ์คํธ, ๋ชจ๋ OS๋ฅผ ์ง์ํ๊ณ ์๋ค.
TypeScript๋ ์ผ๋ฐ์ ์ธ JavaScript๋ก ์ปดํ์ผ๋๋ ์๋ฐ ์คํฌ๋ฆฝํธ์ Superset์ด๋ค.
์ฆ, ์ง๊ธ๊น์ง ์ฌ์ฉํ๋ ์ต์ํ JavaScript์ ๋ฌธ๋ฒ์ ์ฌ์ฉํ๋ฉด์๋ ์ฝ๋ฉ์ด ๊ฐ๋ฅํ๋ค.
ํนํ, TypeScript๋ ES2015 ๋ฌธ๋ฒ๋ ์ง์ํ๋ฏ๋ก TypeScript ์ด์ธ์ ๋ณ๋ Transpiler๋ฅผ ์ฌ์ฉํ์ง ์์๋ ES2015 ๊ธฐ๋ฅ๋ค์ ๋ธ๋ผ์ฐ์ ์์ ์ฌ์ฉํ ์ ์๋ค๋ ์ฅ์ ๋ ์๋ค.
๊ฒ๋ค๊ฐ ๋ฏธ๋์ ECMAScript Feature๋ค๋ ๊ณ์ํด์ ์ง์ํ ์์ ์ด๋ฏ๋ก ํ์ค์์ ๋ฒ์ด๋ ๊ฑฑ์ ๋ ๋ ์ ์๊ฒ ๋ค.
- ์ฝ๋ฉ์ค ๋ฐ์๋๋ ์ค๋ฅ
var number = "๋ฌธ์์ด";
var str = number - 10; // ์ค๋ฅ๊ฐ ๋ฐ์๋์ง ์์.
typeof str? ๋ฌธ์์ผ๊น์ ์ซ์์ผ๊น์......
'' == false // true
undefined == null // true
false == null // false
var one = 1;
var oneString = "1";
if(one == oneString){
alert("์๋ก ๊ฐ๋ค.");
}
- ์์์ ๋ฌธ์ ์ - ๋ณต์ก๋
function Car(){ /* code.... */ }
function Truck = function(){
// consructor chain
Car.apply(this, arguments);
/* Some code ... */
}
Truck.prototype = new Car();
var porter = new Truck();
- ๋ถ์คํ IDE
- Code Assist
- Syntax Warning
- Code Tracking ๋ถ๊ฐ
- Code Refactoring ๋ถ๊ฐ
Angular 1.x ๋ฒ์ ์์์ ์๋ฐ์คํฌ๋ฆฝํธ ์์ฑ์์ object๋ method์ ๋ํ ์ ์ฐจ๊ฐ ํ๋ฆฐ ๊ฒฝ์ฐ๋ ํ๋ณํ ๋ฌธ์ ์ ๋ฑ์ด
runtime์์๋ง ๋ฐ๊ฒฌ๋๋ ๋ฌธ์ ๊ฐ ์์์ผ๋ฉฐ, ์์ ์ฌํญ์ ๋ฐฉ์งํ๊ธฐ ์ํด ๋จ์ ํ
์คํธ์ ๋ณด๋ค ๋ง์ ์ฝ๋์ ์๊ฐ์ ๋ค์ฌ์ ์์ฑํ์์ง๋ง,
์ฌ์ ํ ๋ฌธ์ ์ ์ด ๋ฐ์๋์๋ค.
๊ทธ๋์ Angular๋ ํ์
์คํฌ๋ฆฝํธ๋ฅผ ๋์
ํ๊ฒ ๋์๊ณ , ์ ์ ํ์ดํ์ ์ฌ์ฉํจ์ผ๋ก์ ์ปดํ์ผ์์ ์๋ชป ์ ์ธ๋ ์ฝ๋๋ฅผ ๋ฐ๊ฒฌํ์ฌ ์ค๋ฅ๋ฅผ ๋ฏธ์ฐ์ ๋ฐฉ์งํ๊ณ ์ ํ๋ค.
- Type annotations
Type annotaion์ function์ด๋ ๋ณ์๋ฅผ ๊ธฐ์ ํ๋ ๋ถ๋ถ์ ๋ด๋นํ๋ค.
// ๋ณ์์ ์
const name: string = 'NKIA';
// ํจ์์ ์
function echo(param: string): string {
return param;
}
// Return Type : void
function warnUser(): void {
alert("This is my warning message");
}
// ํ์
๊ณผ ๋ณ์์ ํ ๋น๊ฐ์ด ๋ง์ง ์๋ ๊ฒฝ์ฐ์ ์๋ฌ ์์. => ์ปดํ์ผ์ ์๋์ ๊ฐ์ ์ค๋ฅ ๋ฐ์๊ฐ ๋ณด์ฌ์ง๋ฉฐ, ์ปดํ์ผ๋์ง ์๋๋ค.
error TS2322: Type 'string' is not assignable to type 'number'
- Interfaces
์๋ฐ์ interface์ ์ ์ฌํ ๊ฐ๋ ์ด๋ฉฐ, ๋ณ์์ method์ ์ ์ธ๋ถ๋ง ๊ฐ์ง๊ณ ์๋ ์ญํ ์ ํ๋ค.
์ค์ ๊ตฌํ์ implements ํค์๋๋ก ๊ตฌํ๋ถ๋ฅผ ์์ฑํ๋ค.
interface Shape {
getArea(): number;
}
class Rect implements Shape {
width : number;
height: number;
constructor(width, height) {
this.width = width;
this.height = height;
}
// Method ๊ตฌํ๋ถ, ๊ตฌํ๋ถ๊ฐ ์์ผ๋ฉด ์๋ฌ๊ฐ ๋ฐ์ํ๋ค.
// error: Class 'Rect' incorrectly implements interface 'Shape'. Property 'getArea' is missing in type 'Rect'.
getArea(){
return 1;
}
}
// interface ๊ฐ ์์ ์ง์. ๋ค์ค์์๋ ๊ฐ๋ฅํ๋ค.
interface Shape {
color: string;
}
interface Square extends Shape {
sideLength: number;
}
var square: Square;
square.color = "blue";
square.sideLength = 10;
// optional ํํ ? ์ฌ์ฉ. class์์ ๊ตฌํํ๊ฑฐ๋ ์ฌ์ฉํ์ง ์์๋ ๋๋ค.
interface SquareConfig {
color?: string;
width?: number;
}
// Javascript ์ฝ๋
class Duck {
quack() {
console.log('๊ฝฅ!');
}
}
class Person {
/* ๊ตฌํ์ด ์๋์ด ์๋ ๊ฒฝ์ฐ..
quack() {
console.log('๋๋ ๊ฝฅ!');
}
*/
}
function makeSomeNoiseWith(duck) {
duck.quack();
}
makeSomeNoiseWith(new Duck()); // OK
makeSomeNoiseWith(new Person()); // OK
-- error: quack() is not a function. ์๋ฌ๊ฐ Runtime์์ ๋ฐ์ํ๋ ๋ถ์์ฌ....
// TypeScript ์ฝ๋
interface Quackable {
quack(): void;
}
class Duck implements Quackable {
quack() {
console.log('๊ฝฅ!');
}
}
class Person {
/*
quack() {
console.log('๋๋ ๊ฝฅ!');
}
*/
}
function makeSomeNoiseWith(duck: Quackable): void {
duck.quack();
}
makeSomeNoiseWith(new Duck()); // OK
makeSomeNoiseWith(new Person()); // OK
-- Quackable์ Person ๊ฐ์ฒด์ quack()๋ฅผ ๊ตฌํํ์ง ์์ ์ปดํ์ผ์์ ์๋ฌ๊ฐ ๋ฐ์ํ๋ค.
- Classes
es2015 ๊ธฐ๋ฐ์ class ์ง์, ๋ชจ๋ํ๋ฅผ ํ ์ ์๋ ํจ์๋ฅผ ์์ฑํ๊ณ ๊ด๋ฆฌํ๋ค.
์ ๊ทผ ์ ํ์, static, getter, setter ๋ฑ์ ์ง์ํ๋ค.
// ๊ธฐ๋ณธ๋ฌธ๋ฒ
class Cat {
name: string;
age: number;
constructor(name: string, age: number) {
this.name = name;
this.age = age;
}
gatName(){
return this.name;
}
}
// ์ปดํ์ผ๋ Javascript ์ฝ๋
var Cat = (function(){
function Cat(name, age){
this.name = name;
this.age = age;
}
Cat.prototype.getName = function(){
return this.name;
};
return Cat;
})();
// ์ ๊ทผ ์ ํ์
class Cat {
private name: string;
private age: number;
constructor(name: string, age: number) {
this.name = name;
this.age = age;
},
getName(): string {
return this.name;
}
}
- Generics
Generic ์ ์ธ์ผ๋ก ๋ฏธ๋ฆฌ ๋ฐ์ ๋ณ์๋ฅผ ์ง์ ํ ์ ์๋ค.
// ๋ฏธ๋ฆฌ ๋ฐ์ ๋ณ์๋ฅผ ์ง์ ํ๋ generic
var list:Array<number> = [1, 2, 3];
function identity<T>(arg: T): T {
return arg;
}
- Enums
์ด๊ฑฐ ์๋ฃํ(enumerated type)์ ๊ณ ์ ๊ฐ์์ ์์๋ค๋ก ๊ฐ์ด ๊ตฌ์ฑ๋๋ ์๋ฃํ์ ์ง์ํ๋ค.
const enum Directions {
Up,
Down,
Left,
Right
}
let directions = [Directions.Up, Directions.Down, Directions.Left, Directions.Right]
- Declration File
๋ผ์ด๋ธ๋ฌ๋ฆฌ ๋ช ์ธ์ ํ์ผ์ด๋ฉฐ, ํ์ฅ์๋ .d.ts๋ก ๋์ด ์๋ค. tsconfig.json์ ๊ธฐ์ ํ๊ฑฐ๋, reference๋ก ๋ก๋ํ ์ ์๋ค.
๋ฏธ๋ฆฌ ์ ์๋์ด ์์ง ์๋ ์ธ๋ถ ๋ผ์ด๋ธ๋ฌ๋ฆฌ๋ฅผ ์ฌ์ฉํ๋ ค๋ฉด, Declartion File์ ์ฐธ์กฐํ๊ฒ ํด์ฃผ์ด์ผ์ง ์๋ฌ๊ฐ ๋ฐ์ํ์ง ์๋๋ค.
typings : https://github.com/typings/typings
definity : http://definitelytyped.org/
// jquey datepicker ์ฌ์ฉ ์์.
interface JQueryStatic {
datepicker: () => any;
}
// declare๋ก ์ ์ธํ์ฌ ์ฌ์ฉ. ์ ์ธํ์ง ์์ผ๋ฉด $ is not undefined ์๋ฌ๊ฐ ๋ฐ์ํ๋ค.
declare var jQuery: JQueryStatic;
declare var $: JQueryStatic;
Javascript๋ก ์์ฑ๋ ์ฝ๋
jQuery.ajax(url, settings);
์์ ์ฝ๋๋ฅผ ๋ณด๋ฉด url๊ณผ settings๋ผ๋ ๋ณ์๋ฅผ ์ ๊ณตํ๊ณ ์๋ค.
url์ธ ๋ณ์๋ ์๋ง string์ผ๋ก ์ถ๋ก ์ ํ ์ ์์ผ๋ฉฐ, setting ๋ณ์๋ ๊ฐ์ฒด๋ผ๊ณ ์ถ๋ก ์ ํ ์ ์์ง๋ง, ํ๋ฆด ์๋ ์๋ค.
Javascript์์ ์ ๊ณต๋๋ code๋ง์ผ๋ก๋ ํ์์ด๋, return์ ๋ํ ์ ๋ณด๋ฅผ ์์ธกํ๊ฑฐ๋ ์์๊ฐ ์๋ค. ์ฆ, ์๋ ค์ง ์ ๋ณด๊ฐ ์๋ฌด๊ฒ๋ ์๋ค. ๋จ์ง ์ถ๋ก ์ผ ๋ฟ์ด๋ค. ajax function์ ์ฌ์ฉํ๋ ค๋ฉด ๋ฌธ์ ๋๋ ์ ๊ณต๋๋ ์ฝ๋๋ฅผ ๋ด์ผํ๋ฉฐ, ์ด ๋ฌธ์๊ฐ ์์ ๋ฒ์ ์ ๋ฌธ์์ผ ์ ๋ ์๋ค.
Typescript๋ก ์์ฑ๋ ์ฝ๋
ajax(url: string, settings?: JQueryAjaxSettings): JQueryXHR;
interface JQueryAjaxSettings {
async?: boolean;
cache?: boolean;
contentType?: any;
headers?: { [key: string]: any; };
//...
}
interface JQueryXHR {
responseJSON?: any; //...
}
ํจ์ ์์ฒด๋ง ๋ณด๋๋ผ๋ url์ string type์ด๋ฉฐ, setting๋ optionalํ JQueryAjaxSettings type์ผ๋ก ๋์ด ์๋ค.
๋ํ, ์ด ํจ์๋ JQueryXHR์ returnํ๊ณ ์๋ ๊ฒ์ ํ์ธ ํ ์ ์๋ค.
Javascript๋ก ์์ฑ๋ ์ฝ๋์ ๋นํด ๊ตฌํ ์ฝ๋๊ฐ ๋ ๊ธธ์ด์ก์ง๋ง, ์ด์ ๊ฐ๋ฐ์๋ค์ด ๋ฌธ์๋ ์ค์ ๊ตฌํ ์ฝ๋๋ฅผ ํ์ธํ์ง ์๊ณ ๋,
์ฝ๋์ ๋ํ ์ดํด๋์ ๊ฐ๋
์ฑ์ ๋์ฌ์ฃผ๋ ์์ฉ์ ํ๊ณ ์๋ค.
---- Next Step ----
- npm์ด ๊ธฐ๋ณธ์ผ๋ก ์ค์น๋์ด ์์ด์ผ ํ๋ค.
- npm install -g typescript
ํ์ผ์ .ts ํ์ฅ์๋ก ์์ฑํด์ผ ํ๋ฉฐ, tsc ์ปค๋งจ๋๋ก ์ปดํ์ผํ๋ฉด .js ํ์ผ๋ก ๋น๋(์์ฑ)๋๋ค.
TypeScrpt Definition Manager์ ์ฝ์๋ก, Declaration File ๊ด๋ฆฌ๋ฅผ ์ง์ํ๋ค.
npm install -g tsd
tsd install jquery