MS가 만든 타입 체크가 가능한 스크립트 - TypeScript


안녕하세요 수빈빠 입니다.

이것저것 보다가 재밌는게(MS가 했다는게?) 있어서 공유합니다.

 

요새 워낙 Script language가 대세라(구글 dart도 그렇고, CoffeeScript도 그렇고)

MS도 "TypeScript"라는 걸 내놨습니다. ( http://www.typescriptlang.org/ )

 

http://www.typescriptlang.org/Playground/ 여기에서는 온라인으로 컴파일을 해 볼 수 있습니다.

 

1. 개념

간단히 설명을 하자면(물론 더 많은 기능이 숨어 있는 것 같은데 거기까지는 못 봤네요)

type을 체크할 수 있는 문법을 두어서 개발을 한 후, 이것을 javascript로 pre-compile하는 방식입니다.

 

2. 환경

에디터는 vim, emacs, subtime text 같은 것을 지원하고요..

컴파일은 node.js나 visual studio를 사용하는 방식입니다.

node의 경우, npm을 통해서 "npm install -g typescript" 하면 바로 설치가 가능합니다.

컴파일의 경우에는 "tsc aaaa.ts" 하면 js가 만들어집니다.

 

3. 사용 범위

문서에 보면 어떤 javascript host도 가능하다고 되어 있습니다.

주로 예제는 브라우저용이기는 한데, node용 스크립트도 됩니다.

다만 일부 global wide한 내장 변수의 경우 경고를 뱉기는 합니다만, 컴파일은 됩니다.

 

4. 예제

예제를 봐야겠죠? 먼저 컴파일이 실패하는 예제 입니다.

 

interface Person {
    firstname: string;
    lastname: string;
}

function greeter(person : Person) {
    return "Hello, " + person.firstname + " " + person.lastname;
}

var user = {firstname: 123, lastname: "User"};

console.log(greeter(user));
 

보시면 바로 아실 겁니다. type이 맞지 않죠? 아래와 같은 오류가 발생합니다.

경축! 아무것도 안하여 에스천사게임즈가 새로운 모습으로 재오픈 하였습니다.
어린이용이며, 설치가 필요없는 브라우저 게임입니다.
https://s1004games.com

 

person-error.ts(12,12): Supplied parameters do not match any signature of call target:
        Types of property 'firstname' of types '{ firstname: number; lastname: string; }' and 'Person' are incompatible

 

요번에는 성공하는 예제 입니다. 왼쪽은 TypeScript, 오른쪽은 JavaScript 입니다.

 

interface Person {
    firstname: string;
    age: number;
}

function greeter(person : Person) {
    return "Hello, " + person.firstname + " " + person.age;
}

var user = {firstname: "Jane", age: 20};

console.log(greeter(user));

var user2 = {firstname : "", age : 0}

process.argv.forEach(function (val, index, array) {
    if(index == 2) {
        user2.firstname = val;
    }   
    if(index == 3) {
        user2.age = val;
    }
})

console.log(greeter(user2));

function greeter(person) {
    return "Hello, " + person.firstname + " " + person.age;
}
var user = {
    firstname: "Jane",
    age: 20
};
console.log(greeter(user));
var user2 = {
    firstname: "",
    age: 0
};
process.argv.forEach(function (val, index, array) {
    if(index == 2) {
        user2.firstname = val;
    }
    if(index == 3) {
        user2.age = val;
    }
});
console.log(greeter(user2));
 

 

딱 결과를 보시면 아시겠지만, javascript 쪽에는 type check를 위한 코드가 만들어지지 않습니다.

즉 compile time에만 체크하고, run time에는 체크하지 않습니다.

 

위의 javascript를 node에서 실행할 경우...

 

node person.js First fasdfasd => Number가 아님에도 이렇게 실행해도 잘 되고

node person.js First 12345 => 이렇게 실행해도 잘 됩니다.

 

5. 기타 등등

type check를 하지 않는 script language는 양날의 검이죠.

생산성이 올라가지만, 디버깅 하기 어려운...

TypeScript는 이 문제를 잡겠다고 했지만, 좀 부족한 면이 있습니다.

 

예전에(지금은 거의 쓰지 않지만) commons-validator 모듈을 사용하면

client javascript 까지 제공해 주어서, 사용자의 값을 체크할 수 있었는데,

TypeScript의 경우에는 그런 면이 조금 아쉽습니다.

 

[출처] http://okky.kr/article/205297

 

 

본 웹사이트는 광고를 포함하고 있습니다.
광고 클릭에서 발생하는 수익금은 모두 웹사이트 서버의 유지 및 관리, 그리고 기술 콘텐츠 향상을 위해 쓰여집니다.
번호 제목 글쓴이 날짜 조회 수
438 [watcher 설계] Observer Design Pattern file 졸리운_곰 2016.05.30 94
437 [software watchdog] Acelet-Scheduler file 졸리운_곰 2016.05.30 79
436 [software watchdog] Amplify::Failover file 졸리운_곰 2016.05.30 67
435 2016 Mobile App Trends 졸리운_곰 2016.05.29 1448
434 5 big enterprise application development trends of 2016 file 졸리운_곰 2016.05.29 207
433 Native mobile and DevOps will be a continued focus, and "cloud computing" will become simply "computing" file 졸리운_곰 2016.05.29 77
432 Latest Mobile App Trends in 2016 file 졸리운_곰 2016.05.29 428
431 4 Hot Mobile Marketing Trends to Watch in 2016 졸리운_곰 2016.05.29 132
430 2016 Mobile And App Marketing Trends 졸리운_곰 2016.05.29 819
429 TinyBasic (Java, Flex, C#) file 졸리운_곰 2016.05.21 167
428 Open Textbooks and Materials for Open Source Education 졸리운_곰 2016.05.17 357
427 비트코인 [BitCoin] file 졸리운_곰 2016.05.15 480
426 TypeScript 환경 설정 및 테스트 졸리운_곰 2016.05.15 166
425 타입스크립트 (TypeScript) 기초강좌 file 졸리운_곰 2016.05.15 196
» MS가 만든 타입 체크가 가능한 스크립트 - TypeScript 졸리운_곰 2016.05.15 83
423 ESXi 5.x Installation on Intel NUC fails with “No Network Adapters” file 졸리운_곰 2016.05.14 175
422 VMware Homeserver – ESXi on 4th Gen Intel NUC file 졸리운_곰 2016.05.14 123
421 윈도우에서 swift 프로그래밍 / Programming Swift on Windows 졸리운_곰 2016.05.11 232
420 Big Data Hadoop Alternatives: What They Offer and Who Uses Them : 맵리듀스와 하둡의 대안 찾기 file 졸리운_곰 2016.05.06 289
419 BECOME AN INFOPRENEUR : 정보제공사업자 되기 file 졸리운_곰 2016.05.06 198
대표 김성준 주소 : 경기 용인 분당수지 U타워 등록번호 : 142-07-27414
통신판매업 신고 : 제2012-용인수지-0185호 출판업 신고 : 수지구청 제 123호 개인정보보호최고책임자 : 김성준 sjkim70@stechstar.com
대표전화 : 010-4589-2193 [fax] 02-6280-1294 COPYRIGHT(C) stechstar.com ALL RIGHTS RESERVED