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.
I have a class like this but I want to initialize a default value for Id and email when there is no data. How do I do that? As you see, I want to set the default value if I pass new Recipient ()
const user = this.options.find(x=>x.id == 1);
return user? new Recipient (id:user.id, email:user.email): new Recipient ();
export class Recipient {
id: number;
email?: string;
constructor(data?: Partial<Recipient>) {
Object.assign(this, data);
}
}=======================Update======================
eventually I chose one of two options
export class Recipient {
id: number;
email?: string;
constructor(data?: Partial<Recipient>) {
this.id = data?.id || 0;
this.email = data?.email || null;
}
}
export class Recipient {
id: number;
email?: string;
constructor(data?: Partial<Recipient>) {
Object.assign(this,{id:0, email :null}, data);
}
}
How to set a variable if undefined in typescript? - Stack Overflow
Defaulting Unspecified Interface Properties to Null in TypeScript - Ask a Question - TestMu AI Community
Property with an initial default value can get a undefined excluding TypeScript type while still becoming undefined during runtime
What's the best way to iterate on objects with dynamic fields and avoid the "Object is possibly 'undefined'" error?
The logical nullish assignment (x ??= y) operator only assigns if x is nullish (null or undefined).
x ??= default_value
TypeScript can tell that x is definitely not defined, because it is a block-scoped variable and you can see the whole block.
If you know better than the compiler, you can separate the lines:
const def_val = 'default';
let x: string;
x = (typeof x === 'undefined') ? def_val : x;
But you might want to consider how the block scoped variable could possibly be undefined in your case (perhaps your code is not quite as simple as the example in your question).
The common use case would be more like:
const def_val = 'default';
function work(y: string) {
let x = (typeof y === 'undefined') ? def_val : y;
}
You can also add more strict compiler options to make it less likely the value will be undefined in many cases.
Shorthand
There is also a shorthand falsey-coalesce that may be useful:
const def_val = 'default';
function work(y: string) {
let x = y || def_val;
}
This will replace undefined, null, 0 (zero) or '' with the default.