'Class constructor optional parameter
I want to be able to initialise a class with or without value. If you pass a value then all methods of this class will expect this value as an argument and the value will be of the type you passed with initialisation.
If you pass nothing then methods are not going to expect any arguments and the value will be undefined.
I think some code will make a better example:
class Foo<T = void> {
constructor(public type?: T) {}
bar = (type: T) => {
if (type) {
this.type = type;
}
};
}
const fooUndefined = new Foo();
fooUndefined.bar(); // no errors, this is ok
fooUndefined.type === undefined; // no errors, this is ok
fooUndefined.bar(1); // expected error, this is ok
const fooNumber = new Foo(0);
fooNumber.type === 1; // no errors, but type is `number | undefined`, this is not ok
fooNumber.type > 0; // unexpected error because type is `number | undefined`, this is not ok
fooNumber.bar(1); // no errors, this is ok
fooNumber.bar(); // expected error, need to pass number, this is ok
fooNumber.bar('1'); // expected error, strings are not acceptable, this is ok
So the type
property in fooNumber
example is of type number | undefined
. Is there a way to narrow it to number without explicit typecasting?
Solution 1:[1]
All condition params get SomeType | undefined.
You can write this way to bypass it.
Casting to number
fooNumber.type as number > 2
Or tell typescript you are sure there is available value using ! expression.
fooNumber.type! > 2
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
Solution | Source |
---|---|
Solution 1 | Vinicius Silveira Alves |