Skip to content

Instantly share code, notes, and snippets.

@SnowMasaya
Last active October 11, 2017 02:12
Show Gist options
  • Save SnowMasaya/570e80a67eb6ff4c0a642c6deff4aad7 to your computer and use it in GitHub Desktop.
Save SnowMasaya/570e80a67eb6ff4c0a642c6deff4aad7 to your computer and use it in GitHub Desktop.
npm install -g typescript
let mail: string = '[email protected]';
let msg = `レビューアー募集中!
書籍の感想下さい。 ${mail}`;
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());
interface Car {
type: string;
run(): void;
}
let c: Car = {
type: 'トラック',
run() {
console.log(`${this.type} is run`);
}
};
c.run();
let c1: {type: string, width: number} = {type: '軽トラック', weight: 750};
interface Car2 {
(type: string) : string;
}
let c2: Car2 = function(type: string): string {
return `Car type is ${this}`;
}
let data: Array<number> = [1, 2, 3]
class MyGenerics<T> {
value: T;
getValue(): T {
return this.value;
}
}
let g = new MyGenerics<string>();
g.value = 'Hoge';
console.log(g.getValue());
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());
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));
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);
let triangle = function(base: number, height:number): number {
return base * height / 2;
}
console.log(triangle(10, 5));
let triangle = (base: number, height: number): number => {
return base * height / 2;
};
console.log(triangle(10, 5));
function square(value: number): number | boolean {
if (value < 0){
return false;
} else {
return Math.sqrt(value);
}
}
console.log(square(9));
console.log(square(-9));
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())
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));
namespace Wings.MainApp {
export class Hoge {}
export function foo {}
}
let h = new Wings.MainApp.Hoge();
Wings.MainApp.foo();
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());
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