Closed as not planned
Description
π Search Terms
setter union variance
is:issue in-title: setter (nothing relevant found)
is:issue in-title: setter union variance (nothing relevant found)
π Version & Regression Information
confirmed 4.3.5 ~ 5.3.2
β― Playground Link
π» Code
interface Setter<T> {
set prop(x:T); // runtime error if x not in domain T.
}
class C12 implements Setter<{a:1|2}>{
set prop(x:{a:1}|{a:2}){
if (![1,2].includes(x.a)) throw x;
}
}
class C23 implements Setter<{a:3|2}>{
set prop(x:{a:3}|{a:2}){
if (![2,3].includes(x.a)) throw x;
}
}
function fu2(u: C12 | C23) {
// here "prop" is treated like plain property
// if it were treated like a plain function, {a:1}, {a:3} would be errors
u.prop; // shows 1|2|3 // UNSOUND
u.prop = {a:1}; // possible throw // UNSOUND
u.prop = {a:2};
u.prop = {a:3}; // possible throw // UNSOUND
}
function fu3(u: C12) {
u.prop = {a:2};
if (u.prop.a===2){
//
}
}
fu2(new C12());
// playground output
// [ERR]: "Executed JavaScript Failed:"
// [ERR]: 3
fu3(new C12());
// playground output
// [ERR]: "Executed JavaScript Failed:"
// [ERR]: u.prop is undefined
π Actual behavior
- TypeScript doesn't detect obvious runtime errors that it would detect if setters were treated as functions.
- TypeScript doesn't treat a missing getter as returning undefined.
π Expected behavior
- setters should be treated as functions
- call to missing getters should return undefined
Additional information about the issue
This supersedes issue #56894.
@Anadarist 's pull #56895 claims to fix #56894, it would be great if it fixed this issue also.