alexeyraspopov / dataclass

Data classes for TypeScript & JavaScript
https://dataclass.js.org
ISC License
181 stars 6 forks source link

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

Closed salimfadhley closed 5 years ago

salimfadhley commented 5 years ago

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?

spockoyno commented 5 years ago

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

alexeyraspopov commented 5 years ago

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.