alexeyraspopov / dataclass

Data classes for TypeScript & JavaScript

Home Page:https://dataclass.js.org

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Is it possible to define compulsory fields (without defaults)?

salimfadhley opened this issue · comments

I want to define a simple Record in which all fields are compulsory. This code complies fine:

import Record from 'dataclass'

class Location extends Record<Location> {
    name: string = '';
    address: string = '';
}

But what I'd really like to do is this:

import Record from 'dataclass'

class Location extends Record<Location> {
    name: string;
    address: string;
}

Where I don't provide a default value to my compulsory fields. Can this be done?

This definition would be very useful eg to avoid invalid dataclass instances.

In JavaScript it's not possible because Babel removes class fields with no value as "Flow type annotation". The same happens in TypeScript. This means, there are no "keys" to operate in runtime.