Emi boolean data type
Given the following example:
name: MyBoolClass
fields:
- name: boolWithDefault
default: true
type: bool
- name: plainBool
type: bool
bool and bool? map directly to JavaScript's boolean type.
boolfields are always required and default tofalseunless another default is provided.bool?fields allow bothnullandundefined, withundefinedbeing the default if not specified.
Just like with strings, Emi generates a private backing field (#field) and a pair of getters and setters.
This ensures type safety and avoids accidental runtime errors.
Example with a non-nullable bool:
/**
* The base class definition for myBoolClassDto
**/
export class MyBoolClassDto {
/**
*
* @type {boolean}
**/
#boolWithDefault: boolean = true;
/**
*
* @returns {boolean}
**/
get boolWithDefault() {
return this.#boolWithDefault;
}
/**
*
* @type {boolean}
**/
set boolWithDefault(value: boolean) {
this.#boolWithDefault = Boolean(value);
}
setBoolWithDefault(value: boolean) {
this.boolWithDefault = value;
return this;
}
/**
*
* @type {boolean}
**/
#plainBool!: boolean;
/**
*
* @returns {boolean}
**/
get plainBool() {
return this.#plainBool;
}
/**
*
* @type {boolean}
**/
set plainBool(value: boolean) {
this.#plainBool = Boolean(value);
}
setPlainBool(value: boolean) {
this.plainBool = value;
return this;
}
constructor(data: unknown = undefined) {
if (data === null || data === undefined) {
return;
}
if (typeof data === "string") {
this.applyFromObject(JSON.parse(data));
} else if (this.#isJsonAppliable(data)) {
this.applyFromObject(data);
} else {
throw new Error(
"Instance cannot be created on an unknown value, check the content being passed. got: " +
typeof data,
);
}
}
#isJsonAppliable(obj: unknown) {
const g = globalThis as unknown as { Buffer: any; Blob: any };
const isBuffer =
typeof g.Buffer !== "undefined" &&
typeof g.Buffer.isBuffer === "function" &&
g.Buffer.isBuffer(obj);
const isBlob = typeof g.Blob !== "undefined" && obj instanceof g.Blob;
return (
obj &&
typeof obj === "object" &&
!Array.isArray(obj) &&
!isBuffer &&
!(obj instanceof ArrayBuffer) &&
!isBlob
);
}
/**
* casts the fields of a javascript object into the class properties one by one
**/
applyFromObject(data = {}) {
const d = data as Partial<MyBoolClassDto>;
if (d.boolWithDefault !== undefined) {
this.boolWithDefault = d.boolWithDefault;
}
if (d.plainBool !== undefined) {
this.plainBool = d.plainBool;
}
}
/**
* Special toJSON override, since the field are private,
* Json stringify won't see them unless we mention it explicitly.
**/
toJSON() {
return {
boolWithDefault: this.#boolWithDefault,
plainBool: this.#plainBool,
};
}
toString() {
return JSON.stringify(this);
}
static get Fields() {
return {
boolWithDefault: "boolWithDefault",
plainBool: "plainBool",
};
}
/**
* Creates an instance of MyBoolClassDto, and possibleDtoObject
* needs to satisfy the type requirement fully, otherwise typescript compile would
* be complaining.
**/
static from(possibleDtoObject: MyBoolClassDtoType) {
return new MyBoolClassDto(possibleDtoObject);
}
/**
* Creates an instance of MyBoolClassDto, and partialDtoObject
* needs to satisfy the type, but partially, and rest of the content would
* be constructed according to data types and nullability.
**/
static with(partialDtoObject: PartialDeep<MyBoolClassDtoType>) {
return new MyBoolClassDto(partialDtoObject);
}
copyWith(
partial: PartialDeep<MyBoolClassDtoType>,
): InstanceType<typeof MyBoolClassDto> {
return new MyBoolClassDto({ ...this.toJSON(), ...partial });
}
clone(): InstanceType<typeof MyBoolClassDto> {
return new MyBoolClassDto(this.toJSON());
}
}
export abstract class MyBoolClassDtoFactory {
abstract create(data: unknown): MyBoolClassDto;
}
type PartialDeep<T> = {
[P in keyof T]?: T[P] extends Array<infer U>
? Array<PartialDeep<U>>
: T[P] extends object
? PartialDeep<T[P]>
: T[P];
};
/**
* The base type definition for myBoolClassDto
**/
export type MyBoolClassDtoType = {
/**
*
* @type {boolean}
**/
boolWithDefault: boolean;
/**
*
* @type {boolean}
**/
plainBool: boolean;
};
// eslint-disable-next-line @typescript-eslint/no-namespace
export namespace MyBoolClassDtoType {}
Given the following example:
name: MyBoolClass
fields:
- name: nullableBoolWithDefault
default: true
type: bool?
- name: nullableBoolWithoutDefault
type: bool?
With bool?, the backing field is initialised with undefined.
You can explicitly set it to true, false, or null.
If a default is provided, it will be respected.
/**
* The base class definition for myBoolClassDto
**/
export class MyBoolClassDto {
/**
*
* @type {boolean}
**/
#nullableBoolWithDefault?: boolean | null = true;
/**
*
* @returns {boolean}
**/
get nullableBoolWithDefault() {
return this.#nullableBoolWithDefault;
}
/**
*
* @type {boolean}
**/
set nullableBoolWithDefault(value: boolean | null | undefined) {
const correctType =
value === true ||
value === false ||
value === undefined ||
value === null;
this.#nullableBoolWithDefault = correctType ? value : Boolean(value);
}
setNullableBoolWithDefault(value: boolean | null | undefined) {
this.nullableBoolWithDefault = value;
return this;
}
/**
*
* @type {boolean}
**/
#nullableBoolWithoutDefault?: boolean | null = undefined;
/**
*
* @returns {boolean}
**/
get nullableBoolWithoutDefault() {
return this.#nullableBoolWithoutDefault;
}
/**
*
* @type {boolean}
**/
set nullableBoolWithoutDefault(value: boolean | null | undefined) {
const correctType =
value === true ||
value === false ||
value === undefined ||
value === null;
this.#nullableBoolWithoutDefault = correctType ? value : Boolean(value);
}
setNullableBoolWithoutDefault(value: boolean | null | undefined) {
this.nullableBoolWithoutDefault = value;
return this;
}
constructor(data: unknown = undefined) {
if (data === null || data === undefined) {
return;
}
if (typeof data === "string") {
this.applyFromObject(JSON.parse(data));
} else if (this.#isJsonAppliable(data)) {
this.applyFromObject(data);
} else {
throw new Error(
"Instance cannot be created on an unknown value, check the content being passed. got: " +
typeof data,
);
}
}
#isJsonAppliable(obj: unknown) {
const g = globalThis as unknown as { Buffer: any; Blob: any };
const isBuffer =
typeof g.Buffer !== "undefined" &&
typeof g.Buffer.isBuffer === "function" &&
g.Buffer.isBuffer(obj);
const isBlob = typeof g.Blob !== "undefined" && obj instanceof g.Blob;
return (
obj &&
typeof obj === "object" &&
!Array.isArray(obj) &&
!isBuffer &&
!(obj instanceof ArrayBuffer) &&
!isBlob
);
}
/**
* casts the fields of a javascript object into the class properties one by one
**/
applyFromObject(data = {}) {
const d = data as Partial<MyBoolClassDto>;
if (d.nullableBoolWithDefault !== undefined) {
this.nullableBoolWithDefault = d.nullableBoolWithDefault;
}
if (d.nullableBoolWithoutDefault !== undefined) {
this.nullableBoolWithoutDefault = d.nullableBoolWithoutDefault;
}
}
/**
* Special toJSON override, since the field are private,
* Json stringify won't see them unless we mention it explicitly.
**/
toJSON() {
return {
nullableBoolWithDefault: this.#nullableBoolWithDefault,
nullableBoolWithoutDefault: this.#nullableBoolWithoutDefault,
};
}
toString() {
return JSON.stringify(this);
}
static get Fields() {
return {
nullableBoolWithDefault: "nullableBoolWithDefault",
nullableBoolWithoutDefault: "nullableBoolWithoutDefault",
};
}
/**
* Creates an instance of MyBoolClassDto, and possibleDtoObject
* needs to satisfy the type requirement fully, otherwise typescript compile would
* be complaining.
**/
static from(possibleDtoObject: MyBoolClassDtoType) {
return new MyBoolClassDto(possibleDtoObject);
}
/**
* Creates an instance of MyBoolClassDto, and partialDtoObject
* needs to satisfy the type, but partially, and rest of the content would
* be constructed according to data types and nullability.
**/
static with(partialDtoObject: PartialDeep<MyBoolClassDtoType>) {
return new MyBoolClassDto(partialDtoObject);
}
copyWith(
partial: PartialDeep<MyBoolClassDtoType>,
): InstanceType<typeof MyBoolClassDto> {
return new MyBoolClassDto({ ...this.toJSON(), ...partial });
}
clone(): InstanceType<typeof MyBoolClassDto> {
return new MyBoolClassDto(this.toJSON());
}
}
export abstract class MyBoolClassDtoFactory {
abstract create(data: unknown): MyBoolClassDto;
}
type PartialDeep<T> = {
[P in keyof T]?: T[P] extends Array<infer U>
? Array<PartialDeep<U>>
: T[P] extends object
? PartialDeep<T[P]>
: T[P];
};
/**
* The base type definition for myBoolClassDto
**/
export type MyBoolClassDtoType = {
/**
*
* @type {boolean}
**/
nullableBoolWithDefault?: boolean;
/**
*
* @type {boolean}
**/
nullableBoolWithoutDefault?: boolean;
};
// eslint-disable-next-line @typescript-eslint/no-namespace
export namespace MyBoolClassDtoType {}