Skip to content

Instantly share code, notes, and snippets.

@shinofara
Created July 3, 2013 13:52
Show Gist options
  • Save shinofara/5918011 to your computer and use it in GitHub Desktop.
Save shinofara/5918011 to your computer and use it in GitHub Desktop.
【javascriptを使う人に知って貰いたい(エンジニア、デザイナ問わず)】Typescriptを使ってjavascriptをコンパイルを作成する。 ref: http://qiita.com/shinofara@github/items/4396b713ead5a4ae53b4
$ npm install -g typescript
$ vim sample.ts
class shop {
price: number;
num: number;
constructor() {
this.num = 10;
}
setPrice(price: number) {
this.price = price;
}
calculatePrice() {
var total: number = this.num * this.price;
return this.price + '円のりんごを' + this.num + '個購入した場合' + total + '円です。';
}
}
var sp = new shop();
sp.setPrice(1000);
document.body.innerHTML = sp.calculatePrice();
$ tsc sample.ts
$ ls sample.*
sample.js sample.ts
$ vim sample.js
var shop = (function () {
function shop() {
this.num = 10;
}
shop.prototype.setPrice = function (price) {
this.price = price;
};
shop.prototype.calculatePrice = function () {
var total = this.num * this.price;
return this.price + '円のりんごを' + this.num + '個購入した場合' + total + '円です。';
};
return shop;
})();
var sp = new shop();
sp.setPrice(1000);
document.body.innerHTML = sp.calculatePrice();
$ vim sample.ts
class shop {
price: number;
num: number;
constructor() {
this.num = 10;
}
setPrice(price: number) {
this.price = price;
}
calculatePrice() {
var total: number = this.num * this.price;
return this.price + '円のりんごを' + this.num + '個購入した場合' + total + '円です。';
}
}
var sp = new shop();
sp.setPrice('1000');
document.body.innerHTML = sp.calculatePrice();
$ tsc sample.ts
sample.ts(20,4): error TS2081: Supplied parameters do not match any signature of call target.
sample.ts(20,4): error TS2087: Could not select overload for 'call' expression.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment