Upgrade to Pro — share decks privately, control downloads, hide ads and more …

Sharing type definitions between TypeScript And...

Sharing type definitions between TypeScript And Dart

ikasoumen

June 18, 2019
Tweet

More Decks by ikasoumen

Other Decks in Programming

Transcript

  1. w JLBTPVNFO w UXJUUFS!@JLBTPVNFO w QJYJWJLB@TPVNFO w HJUIVCJLBTPVNFO w ϐΫγϒ෱ԬΦϑΟε

    w 5ZQF4DSJQU͸"OHVMBS͔Β w ࠷ۙ͸3FBDUͰ΋ԿͰ΋54Ͱॻ͘ w Πϥετ΍ອըΛඳ͘ͷ΋޷͖ ࣗݾ঺հ 2
  2. export type UserDoc = { name: string, role: Roles, /**

    * @TJS-type integer */ createdAt: number, } export enum Roles { guest = "guest", member = "member", admin = "admin", } JOQVU5ZQF4DSJQU
  3. class UserDoc { int createdAt; String name; Roles role; UserDoc({

    this.createdAt, this.name, this.role, }); factory UserDoc.fromJson(Map<String, dynamic> json) => new UserDoc( createdAt: json["createdAt"], name: json["name"], role: rolesValues.map[json["role"]], ); Map<String, dynamic> toJson() => { "createdAt": createdAt, "name": name, "role": rolesValues.reverse[role], }; } PVUQVU%BSU 
  4. enum Roles { ADMIN, GUEST, MEMBER } final rolesValues =

    new EnumValues({ "admin": Roles.ADMIN, "guest": Roles.GUEST, "member": Roles.MEMBER }); class EnumValues<T> { Map<String, T> map; Map<T, String> reverseMap; EnumValues(this.map); Map<T, String> get reverse { if (reverseMap == null) { reverseMap = map.map((k, v) => new MapEntry(v, k)); } return reverseMap; } } PVUQVU%BSU