Learning Center

Learning Center

Learn Menu

TypeScript Engine

This engine conforms to the ESLint standard style guide except for semicolons. Since the code is generated, we feel it's safer to include the semicolons.

ES2017

Make sure to set the target and lib to es2017 in your tsconfig.json file. OneGen uses some features that are a part of the es2017, such as Object.entries().

Data Types

Standard

Design TypeScript Default Value
bool boolean false
int number 0
int8 number 0
int16 number 0
int32 number 0
int64 number 0
uint number 0
uint8 number 0
uint16 number 0
uint32 number 0
uint64 number 0
float number 0
double number 0
string string ''
char string ''
byte number 0
bytea Uint8Array null
date Date null
datetime Date null

Complex

Name Default Value
Array []
Object (Map) {}

Config

There are currently no typescript config options available for this engine. It's likely to change in the future based on user feedback.

ESLint Config

If you're just starting a new project and looking for a compatible .eslintrc.js config, you can use the one below.

  module.exports = {
  env: {
    browser: true,
    es2021: true
  },
  extends: [
    'standard'
  ],
  parser: '@typescript-eslint/parser',
  parserOptions: {
    ecmaVersion: 'latest',
    sourceType: 'module'
  },
  plugins: [
    '@typescript-eslint'
  ]
}

Package.json dependencies

You'll also need to add the following devDependencies to your package.json file. Just make sure to update the versions as they may be outdated by the time you read this.

  "devDependencies": {
    "@typescript-eslint/eslint-plugin": "^5.25.0",
    "@typescript-eslint/parser": "^5.25.0",
    "eslint": "^8.15.0",
    "eslint-config-standard": "^17.0.0",
    "eslint-plugin-import": "^2.26.0",
    "eslint-plugin-n": "^15.2.0",
    "eslint-plugin-promise": "^6.0.0",
  }

Enum

Every enum case is generated with an associated value. If a user doesn't define a value, it will be automatically assigned by the following logic:

  • int enum: incrementing values starting from 0
  • string enum: value matches the case id

Serialization

OneGen generates serialization (toPlain) and deserialization (fromPlain) methods for every class. These methods make any JSON-based API integration a breeze.

Let's consider the following example. We have two models, User and Company (which is referenced from the User model). Take a look at the generated serialization methods and how they work.

  export class Company {
  name: string = '';

  static fromPlain (plain: any): Company {
    const company = new Company();
    company.name = plain.name;
    return company;
  }

  toPlain (): any {
    const plain: any = {};
    plain.name = this.name;
    return plain;
  }
}

export class User {
  email: string = '';
  password: string = '';
  isSubscribed: boolean = false;
  created: Date = null;
  company: Company = null;

  static fromPlain (plain: any): User {
    const user = new User();
    user.email = plain.email;
    user.password = plain.password;
    user.isSubscribed = plain.isSubscribed;
    user.created = plain.created ? new Date(plain.created) : null;
    user.company = plain.company ? Company.fromPlain(plain.company) : null;
    return user;
  }

  toPlain (): any {
    const plain: any = {};
    plain.email = this.email;
    plain.password = this.password;
    plain.isSubscribed = this.isSubscribed;
    plain.created = this.created ? this.created.toISOString() : null;
    plain.company = this.company ? this.company.toPlain() : null;
    return plain;
  }
}

fromPlain (deserialize)

This static method takes a plain JavaScript object and returns a new instance of the associated model. It's very easy to use, let's say we want to create a User object from a mocked API response.

  // Imagine the following object is returned from your API
const userAPIResponse = {
  email: 'joe@doe.com',
  password: '',
  isSubscribed: true,
  created: '2022-05-30T21:26:49+00:00',
  company: { name: 'Friendly Corp' } // Notice the plain Company object here
};
const user = User.fromPlain(userAPIResponse);
console.log(user.email); // prints out 'joe@doe.com'
console.log(user.company); // it's an instance of Company now
console.log(user.created); // it's an instance of Date now

toPlain (serialize)

This instance method doesn't accept any parameters and returns a new plain JavaScript object. Let's use the previous example to serialize the User instance into a new plain object.

  const newPlainUser = user.toPlain();
console.log(newPlainUser.company); // it's a plain object now
console.log(newPlainUser.created); // it's an ISO 8601 string now.

Copy/Clone

If you've already noticed, congratulations! Because our serialization methods safely copy data from/to plain objects, we can actually use them to create a deep clone/copy of any model. Check out the example below.

  let user = new User();
user.email = 'john@doe.com';
user.company = Company.fromPlain({ name: 'Cat Cafe, LLC.' });

let clone = User.fromPlain(user.toPlain());
clone.company.name = 'Dog Cafe, LLC.'; // we can just rename the company and leave the rest
console.log(user.company.name); // prints out 'Cat Cafe, LLC.'
console.log(clone.company.name); // prints out 'Dog Cafe, LLC.'

Date format

We expect plain objects to have the date in the ISO 8601 string format. For instance, January 5th, 2022 at 11:45 am would be 2022-01-05T11:45:00.000Z

Nested Entities

As you know, OneGen supports both nested classes and enums in the design module. Unfortunately, TypeScript doesn't support nested classes (or enums), so we had to work around it to generate models you can still comfortably work with.

We basically pluck out the nested entities to the file level and prefix their names. It's best to see it in action - imagine we have a class Person and a nested enum Pet which can be either a dog or a cat.

  export enum PersonPet {
  DOG = 'dog',
  CAT = 'cat',
}

export class Person {
  // ...
}

We use cookies to track activity using Google Analytics & reCAPTCHA. It helps us understand what our visitors like about our product and how they interact with our website. For more information, check out our Privacy Policy.