Created
April 26, 2016 01:14
-
-
Save BlackPrincess/498b1a854f4397aa0094405bc752eb48 to your computer and use it in GitHub Desktop.
ts勉強初日目、なぜなのか
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
export class Id<A> { | |
constructor(public value: Number) { } | |
} | |
export interface Iso<A, B> { | |
to(a: A): B; | |
from(b: B): A; | |
} | |
export class IdIso<A> implements Iso<Number, Id<A>> { | |
public to(a: Number): Id<A> { | |
return new Id<A>(a); | |
} | |
public from(b: Id<A>): Number { | |
return b.value; | |
} | |
} | |
export function idIso<A>() { | |
return new IdIso<A>(); | |
} | |
class User { | |
static getJson(id: Id<User>): User { | |
// dummy | |
return new User(); | |
} | |
} | |
class Member { | |
static getJson(id: Id<Member>): Member { | |
// dummy | |
return new Member(); | |
} | |
} | |
User.getJson(idIso<User>().to(1)); | |
Member.getJson(idIso<Member>().to(1)); | |
// required: Id<User> | |
// found: Id<Member> | |
// but not compile error | |
User.getJson(idIso<Member>().to(1)); | |
// required: Id<Member> | |
// found: Id<User> | |
// but not compile error | |
Member.getJson(idIso<User>().to(1)); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment