#Make setter accept undefined
12 messages · Page 1 of 1 (latest)
Preview:```ts
class Data {
private _test = ""
get test(): string {
return this._test
}
set test(v: string | undefined) {
if (v !== undefined) {
this._test = v
}
}
}
const x = new Data()
x.test = "a"
x.test = undefined
...```
Finally found the issue, it happens when the type is coming from an interface like so:
I ignored that while making an example code
Preview:```ts
interface iface {
member?: string
}
class Data {
private _test = ""
get test(): iface["member"] {
return this._test
}
set test(v: iface["member"]) {
if (v !== undefined) {
this._test = v
}
}
}
...```
And I forgot | undefined here, though the member? does the same thing. This is not the case in my code, huh?!
I'm so confused
Now, whole the issue is gone magically, even this (copy-paste) is not erroring in vscode, which is also basically the same with my original code.
this is playground
Preview:```ts
class Data {
get test(): number {
return 1
}
set test(v) {}
}
let d = new Data()
d.test = undefined
Math.ceil(d.test)```