Skip to content

Instantly share code, notes, and snippets.

@cchantep
Last active February 27, 2019 16:41
Show Gist options
  • Save cchantep/df343806fc826aabdede4833cd06c619 to your computer and use it in GitHub Desktop.
Save cchantep/df343806fc826aabdede4833cd06c619 to your computer and use it in GitHub Desktop.
Scala2TypeScript
sealed trait Animal {
def taxonomyId: Int
}
case class Dog(
color: String,
name: Option[String]) extends Animal {
val taxonomyId = 9615
}
case class Cat(
countries: Seq[Country]) extends Animal {
val taxonomyId = 9685
}
case object Cryptide extends Animal {
val taxonomyId = -1
}
case class Country(name: String, regionCode: Short)
case class Vet[A <: Animal](address: String)
export interface ICat {
countries: ICountry[];
}
export class Cat implements ICat {
public countries: Country[];
constructor(
countries: Country[]
) {
this.countries = countries;
}
public static fromData(data: any): Cat {
return <Cat>(data);
}
public static toData(instance: Cat): any {
return this;
}
}
export interface ICountry {
regionCode: number;
name: string;
}
export class Country implements ICountry {
public regionCode: number;
public name: string;
constructor(
regionCode: number,
name: string
) {
this.regionCode = regionCode;
this.name = name;
}
// Using FieldNaming.SnakeCase
public static fromData(data: any): Country {
return new Country(data.region_code, data.name);
}
// Using FieldNaming.SnakeCase
public static toData(instance: Country): any {
return {
region_code: instance.regionCode,
name: instance.name
};
}
}
export class Cryptide {
private static instance: Cryptide;
private constructor() {}
public static getInstance() {
if (!Cryptide.instance) {
Cryptide.instance = new Cryptide();
}
return Cryptide.instance;
}
}
export interface IDog {
name: (string | null);
color: string;
}
export class Dog implements IDog {
public name: (string | null);
public color: string;
constructor(
name: (string | null),
color: string
) {
this.name = name;
this.color = color;
}
public static fromData(data: any): Dog {
return <Cat>(data);
}
public static toData(instance: Dog): any {
return this;
}
}
export type IAnimal = ICat | ICountry | Cryptide | IDog;
export interface IVet<A> {
address: string;
}
export class Vet<A> implements IVet<A> {
public address: string;
constructor(
address: string
) {
this.address = address;
}
public static fromData<T>(data: any): Vet<A> {
return <Vet<A>>(data);
}
public static toData<T>(instance: Vet<A>): any {
return this;
}
}
@cchantep
Copy link
Author

person_mask

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment