Can I tell the interface to default the properties I don't supply to null? What would let me do this
No. You cannot provide default values for interfaces or type aliases as they are compile time only and default values need runtime support
Alternative
But values that are not specified default to undefined in JavaScript runtimes. So you can mark them as optional:
interface IX {
a: string,
b?: any,
c?: AnotherType
}
And now when you create it you only need to provide a:
let x: IX = {
a: 'abc'
};
You can provide the values as needed:
x.a = 'xyz'
x.b = 123
x.c = new AnotherType()
Answer from basarat on Stack OverflowCan I tell the interface to default the properties I don't supply to null? What would let me do this
No. You cannot provide default values for interfaces or type aliases as they are compile time only and default values need runtime support
Alternative
But values that are not specified default to undefined in JavaScript runtimes. So you can mark them as optional:
interface IX {
a: string,
b?: any,
c?: AnotherType
}
And now when you create it you only need to provide a:
let x: IX = {
a: 'abc'
};
You can provide the values as needed:
x.a = 'xyz'
x.b = 123
x.c = new AnotherType()
You can't set default values in an interface, but you can accomplish what you want to do by using optional properties:
Simply change the interface to:
interface IX {
a: string,
b?: any,
c?: AnotherType
}
You can then do:
let x: IX = {
a: 'abc'
}
And use your init function to assign default values to x.b and x.c if those properies are not set.
Interface Default Values
Default value for interface property?
If you have Object.assign, or a polyfill for it, or something similar like $.extend in jQuery, then you can do:
const settings = Object.assign({}, defaults, options);
The es6.d.ts declaration looks like this:
assign<T, U>(target: T, source: U): T & U; assign<T, U, V>(target: T, source1: U, source2: V): T & U & V; assign<T, U, V, W>(target: T, source1: U, source2: V, source3: W): T & U & V & W; assign(target: any, ...sources: any[]): any;
i.e. strongly-typed overloads up to 4 arguments. So you should get back an object whose type is the intersection of the arguments you passed in.
In short, this will perform defaulting for multiple properties in one go.
More on reddit.comHow to use interface and default parameters together?
Defaulting Unspecified Interface Properties to Null in TypeScript - Ask a Question - TestMu AI Community
Videos
So, I'm playing around with React+TS.
Some Components takes optional style property, and assigns few properties by itself.
Atm I'm using this:
const style = typeof this.props.style === 'object' ? this.props.style : {}; // Default value for styleI'm sure there's simplier way todo that without checking if optional parameter was given.
Thanks in advance!
I'm not sure if the author of the article tried his own code in a playground at least. Official 3.7.2 playground doesn't support it (Playground Link).
It's kinda obvious that it shouldn't work since interfaces aren't for defining values, they are for defining contracts. They help you (and the compiler) wiith type checking but they aren't adding or changing fields of objects that are declared as implementations.
Please read the official documentation first: https://www.typescriptlang.org/docs/handbook/interfaces.html
If you need default values you have to define a class that implements that interface:
interface ClockInterface {
currentTime: Date;
}
class Clock implements ClockInterface {
currentTime: Date = new Date();
constructor(h: number, m: number) { }
}