#Make setter accept undefined

12 messages · Page 1 of 1 (latest)

sweet lily
#

That's not what I'm seeing in the playground....

sleek abyssBOT
#
Gerrit0#7591

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
...```

balmy mulch
#

Finally found the issue, it happens when the type is coming from an interface like so:
I ignored that while making an example code

sleek abyssBOT
#
AlexanderX#3054

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
}
}
}
...```

balmy mulch
#

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

sleek abyssBOT
#
AlexanderX#3054

Preview:```ts
class Data {
get test(): number {
return 1
}
set test(v) {}
}

let d = new Data()
d.test = undefined
Math.ceil(d.test)```

balmy mulch
#

and this is a ts file with 0 errors in vscode

#

I was already expecting it to accept undefined by default for any type, here it randomly worked properly.

#

Ok it seems the inconsistency in my local is because I was testing the original TS class from a JS file with @ts-check