I have an interface that looks like this
export interface IAppSection {
key: string;
order: number;
header: string;
content: string;
modifiedAt: string;
modifiedByEmployeeId: number;
change: 'added' | 'removed' | 'updated' | 'none';
}
What I'd like to do is have change
default as none
when the object this interface relates to is stored.
I have tried change: 'added' | 'removed' | 'updated' | 'none' = 'none'
but this does not work.
I am sure I am doing something wrong here and would really appreciate some feedback on how I can achieve this.
In TypeScript, interfaces represent the shape of an object. They support many different features like optional parameters but unfortunately do not support setting up default values.
To set a default value for a function parameter, use an equal sign right after the parameter name, e.g. function multiply(num: number, by = 10) {} . If a value for the parameter is not provided, the argument will be replaced with the default value.
Use the Partial utility type to make all of the properties in a type optional, e.g. const emp: Partial<Employee> = {}; . The Partial utility type constructs a new type with all properties of the provided type set to optional. Copied!
Interfaces and Inheritance An interface can be extended by other interfaces. In other words, an interface can inherit from other interface. Typescript allows an interface to inherit from multiple interfaces. Use the extends keyword to implement inheritance among interfaces.
You can't do this with interfaces. Interfaces are completely erased at runtime and can't impact runtime behaviour; this is by design. You can create a class instead and assign a default value to the field, or you can create a function that will assign the defaults.
We could even construct a function that helps us create such functions with defaults:
interface IAppSection {
key: string;
order: number;
header: string;
content: string;
modifiedAt: string;
modifiedByEmployeeId: number;
change: 'added' | 'removed' | 'updated' | 'none';
}
function withDefaults<T>() {
return function <TDefaults extends Partial<T>>(defs: TDefaults) {
return function (p: Pick<T, Exclude<keyof T, keyof TDefaults>> & Partial<TDefaults>) :T {
let result: any = p;
for (let k of Object.keys(defs)) {
result[k] = result[k] || defs[k];
}
return result;
}
}
}
const appSection = withDefaults<IAppSection>()({
change: 'none'
})
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With