Last active
October 11, 2017 02:12
-
-
Save SnowMasaya/570e80a67eb6ff4c0a642c6deff4aad7 to your computer and use it in GitHub Desktop.
TypeScript入門 ref: http://qiita.com/GushiSnow/items/8ac6326c02eaf403caac
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
npm install -g typescript |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
let mail: string = '[email protected]'; | |
let msg = `レビューアー募集中! | |
書籍の感想下さい。 ${mail}`; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
interface Figure { | |
getArea(); number; | |
} | |
class Triangle implements Figure { | |
constructor(private width: number, protected height: number) {} | |
getArea(): number { | |
return this.width * this.height / 2; | |
} | |
} | |
let t = new Triangle(10, 5); | |
console.log(t.getArea()); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
interface Car { | |
type: string; | |
run(): void; | |
} | |
let c: Car = { | |
type: 'トラック', | |
run() { | |
console.log(`${this.type} is run`); | |
} | |
}; | |
c.run(); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
let c1: {type: string, width: number} = {type: '軽トラック', weight: 750}; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
interface Car2 { | |
(type: string) : string; | |
} | |
let c2: Car2 = function(type: string): string { | |
return `Car type is ${this}`; | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
let data: Array<number> = [1, 2, 3] |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class MyGenerics<T> { | |
value: T; | |
getValue(): T { | |
return this.value; | |
} | |
} | |
let g = new MyGenerics<string>(); | |
g.value = 'Hoge'; | |
console.log(g.getValue()); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class Hoge {} | |
class FooBar extends Hoge {} | |
class MyGenerics<T extends Hoge> { | |
value: T; | |
getValue(): T { | |
return this.value; | |
} | |
} | |
let g = new MyGenerics<FooBar>(); | |
g.value = new FooBar(); | |
console.log(g.getValue()); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class MyCollection { | |
static addAll<T>(data: T[],... values: T[]): T[] { | |
return data.concat(values); | |
} | |
} | |
let x = [10, 15, 30]; | |
console.log(MyCollection.addAll(x, 35, 50)); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
function merge<T, R>(obj1: T, obj2: R): T & R { | |
let result = <T & R> {}; | |
for(let key in obj1) { | |
(<any>result)[key] = obj1[key]; | |
} | |
for(let key in obj2) { | |
(<any>result)[key] = obj2[key]; | |
} | |
return result; | |
} | |
class Book { | |
constructor(private title: string, private price: number) {} | |
toString() { | |
return this.title + ' ' + this.price; | |
} | |
} | |
class Logger { | |
debug() { | |
console.log(this.toString()); | |
} | |
} | |
let m = merge(new Book('JavaScript Beginner', 2980), new Logger()); | |
console.log(m); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
let triangle = function(base: number, height:number): number { | |
return base * height / 2; | |
} | |
console.log(triangle(10, 5)); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
let triangle = (base: number, height: number): number => { | |
return base * height / 2; | |
}; | |
console.log(triangle(10, 5)); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
function square(value: number): number | boolean { | |
if (value < 0){ | |
return false; | |
} else { | |
return Math.sqrt(value); | |
} | |
} | |
console.log(square(9)); | |
console.log(square(-9)); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class Person { | |
private name: string; | |
private sex: string; | |
constructor(name: string, sex: string) { | |
this.name = name; | |
this.sex = sex; | |
} | |
public show(): string { | |
return `${this.name} ${this.sex}`; | |
} | |
} | |
let p = new Person('Rie', 'Female'); | |
console.log(p.show()) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class Figure { | |
public static PI: number = 3.14159; | |
public static circle(radius: number): number { | |
return radius * radius * this.Pi; | |
} | |
} | |
console.log(Figure.Pi); | |
console.log(Figure.circle(5)); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
namespace Wings.MainApp { | |
export class Hoge {} | |
export function foo {} | |
} | |
let h = new Wings.MainApp.Hoge(); | |
Wings.MainApp.foo(); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class Person { | |
private name: string; | |
private sex: string; | |
constructor(name: string, sex: string) { | |
this.name = name; | |
this.sex = sex; | |
} | |
public show(): string { | |
return `${this.name} ${this.sex}`; | |
} | |
} | |
class BusinessPerson extends Person { | |
work(): string { | |
reutrn `${this.name}`; | |
} | |
} | |
let p = new BusinessPerson('Rie', 'female'); | |
console.log(p.show()); | |
console.log(p.work()); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
abstract class Figure { | |
constructor(protected width: number, protected height:number) {} | |
abstract getArea(): number; | |
} | |
class Triangle extends Figure { | |
getArea(): number { | |
return this.width * this.height / 2; | |
} | |
} | |
let t = new Triangle(10, 5); | |
console.log(t.getArea()); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment