Struct

Type parameters

T: SuperConstructor

T: SuperConstructor

Hierarchy

Implements

Index

Constructors

Methods

Constructors

constructor

+ new Struct(props?: Props): Struct

Creates an instance of Struct. Creates an instance of Struct.

throws {UndefinableClassError} Thrown if provided class constructor has no @define decorator applied.

remarks Property initializers on current implementation of TypeScript v3.7 are counterintuitive when inheritance is involved:

@define('MyClass')
class MyClass extends Struct {
foo = 'default-value';
}
expect(
new MyClass({foo: 'set-value'}).foo
).to.be.equal('set-value'); // false, its 'default-value'

Normally in such cases developer expects, that - since underlying parent of MyClass i.e. Struct class assings properties via Object.assign - they will override the default values of property initializer(so the default-value will be overridden by set-value).

However since MyClass does not override constructor - that will not happen.

This will instantiate MyClass with the default-value, and not the expected one - set-value. Conclusion from this is that property initializers are set AFTER the construction - not before(where inhabitance is in play).

To fix this issue - define custom constructor for derived class:

@define('MyClass')
class MyClass extends Struct {
foo = 'default-value';
constructor(props: Partial<MyClass>) {
super();
Object.assign(this, this.processProps(props));
}
}
expect(
new MyClass({foo: 'set-value'}).foo
).to.be.equal('set-value'); // true

throws {UndefinableClassError} Thrown if provided class constructor has no @define decorator applied.

remarks Property initializers on current implementation of TypeScript v3.7 are counterintuitive when inheritance is involved:

@define('MyClass')
class MyClass extends Struct {
foo = 'default-value';
}
expect(
new MyClass({foo: 'set-value'}).foo
).to.be.equal('set-value'); // false, its 'default-value'

Normally in such cases developer expects, that - since underlying parent of MyClass i.e. Struct class assings properties via Object.assign - they will override the default values of property initializer(so the default-value will be overridden by set-value).

However since MyClass does not override constructor - that will not happen.

This will instantiate MyClass with the default-value, and not the expected one - set-value. Conclusion from this is that property initializers are set AFTER the construction - not before(where inhabitance is in play).

To fix this issue - define custom constructor for derived class:

@define('MyClass')
class MyClass extends Struct {
foo = 'default-value';
constructor(props: Partial<MyClass>) {
super();
Object.assign(this, this.processProps(props));
}
}
expect(
new MyClass({foo: 'set-value'}).foo
).to.be.equal('set-value'); // true

Parameters:

NameTypeDescription
props?PropsProperties of the type required for construction.

Returns: Struct

Methods

equals

equals(other: any): boolean

Implementation of Definable

Inherited from DefinableMixin.equals

Overrides CreateEmployee.equals

Parameters:

NameType
otherany

Returns: boolean


getActions

getActions(): Actions

Implementation of Hookable

Inherited from HookableMixin.getActions

Overrides CreateEmployee.getActions

Returns: Actions


getHook

getHook(action: string, id: string): Hook | undefined

Implementation of Hookable

Inherited from HookableMixin.getHook

Overrides CreateEmployee.getHook

Parameters:

NameType
actionstring
idstring

Returns: Hook | undefined


getHookOrThrow

getHookOrThrow(action: string, id: string): Hook

Implementation of Hookable

Inherited from HookableMixin.getHookOrThrow

Overrides CreateEmployee.getHookOrThrow

Parameters:

NameType
actionstring
idstring

Returns: Hook


getHooks

getHooks(action: string): Mappings

Implementation of Hookable

Inherited from HookableMixin.getHooks

Overrides CreateEmployee.getHooks

Parameters:

NameType
actionstring

Returns: Mappings


getPropTypes

getPropTypes(): Props

Implementation of Definable

Inherited from DefinableMixin.getPropTypes

Overrides CreateEmployee.getPropTypes

Returns: Props


getPropertyInitializers

getPropertyInitializers(): Props

Implementation of Definable

Inherited from DefinableMixin.getPropertyInitializers

Overrides CreateEmployee.getPropertyInitializers

Returns: Props


hasAction

hasAction(action: string): boolean

Implementation of Hookable

Inherited from HookableMixin.hasAction

Overrides CreateEmployee.hasAction

Parameters:

NameType
actionstring

Returns: boolean


hasHook

hasHook(action: string, id: string): boolean

Implementation of Hookable

Inherited from HookableMixin.hasHook

Overrides CreateEmployee.hasHook

Parameters:

NameType
actionstring
idstring

Returns: boolean


overrideHook

overrideHook(action: string, id: string, hook: Hook): void

Implementation of Hookable

Inherited from HookableMixin.overrideHook

Overrides CreateEmployee.overrideHook

Parameters:

NameType
actionstring
idstring
hookHook

Returns: void


registerHook

registerHook(action: string, id: string, hook: Hook, shouldOverride?: boolean): void

Implementation of Hookable

Inherited from HookableMixin.registerHook

Overrides CreateEmployee.registerHook

Parameters:

NameType
actionstring
idstring
hookHook
shouldOverride?boolean

Returns: void


removeHook

removeHook(action: string, id: string): void

Implementation of Hookable

Inherited from HookableMixin.removeHook

Overrides CreateEmployee.removeHook

Parameters:

NameType
actionstring
idstring

Returns: void


toPlainObject

toPlainObject(): Props

Implementation of Definable

Inherited from DefinableMixin.toPlainObject

Overrides CreateEmployee.toPlainObject

Returns: Props


validateProps

validateProps(props: Record‹string | number | symbol, any› | undefined, propTypes: PropTypes, isStrict?: boolean): boolean

Inherited from DefinableMixin.validateProps

Overrides CreateEmployee.validateProps

Parameters:

NameType
propsRecord‹string | number | symbol, any› | undefined
propTypesPropTypes
isStrict?boolean

Returns: boolean


Static getPropTypes

getPropTypes(): Props

Inherited from DefinableMixin.getPropTypes

Overrides CreateEmployee.getPropTypes

Returns: Props


Static getPropertyInitializers

getPropertyInitializers(): Props

Inherited from DefinableMixin.getPropertyInitializers

Overrides CreateEmployee.getPropertyInitializers

Returns: Props