Skip to content

treating setters as properties rather than functions can be unsoundΒ #56922

Closed as not planned
@craigphicks

Description

@craigphicks

πŸ”Ž 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

https://www.typescriptlang.org/play?ssl=16&ssc=78&pln=15&pc=50#code/JYOwLgpgTgZghgYwgAgMoTJKAeAKgPmQG8AoZc5AZw2QAcoB7WgCgA8AuXASgG5kB6fsigBXcMAC2KaIyjJgMZK2QgGYeSGQATBhLihkuAHQkAviQQAbOJUrIAwgEYATPIm1LEKeDvpM0bCI4dkcAH2dTfFIKKhp6JjZ2IJDTUOSIrmiYigVkZgBCAG1HABpnAF0jUCsRLQhKNiM4Li5kMAALRgB3JR4yCnNzKxs7e2cAZjcPLwgfNAwsQODx8Mis8mp1eJYOZPHU9NNM-uz5RQLC5xLxyurLWvrG5taO7t6T5EGSGDEEMGAGJofs5mCJ2A4XMhQg4Jq11gIhO1oCgAETbFHyOxgKAQOCQLTISzAADWKA8+k022gYAAnh9BGd5Oousi2ji8RACUTScg4HRrAYfiA-gCQCViMFHKZxXtTMgugwRJYCQAjaRQWSUD4iIzbPgMyjtBhdOxhZyhcbi0TiKRUI0m5BiOowUCc8gMgCqADlUAB5b0AEW1usYtGQAF4JSl9UJaAxbMAVZ42p1jQjkN6-YHg9sI1GIn0Yjrc5HZTG6PHKInk68056ff6vUHzEKRYDkD9xqDwU5nHCc6G84dCzlzsXQ01w1O+-CYoIPl9gcwQBAer3mC0+gzyTSAOaMJ3IRVgWgiMAkBmFACiACUb+VwSir6wIAgz26AFJwABucFQCCgYBaHUAAxfRPC0dgMQvIRrzvB9kEmEhvhELsVzXFwN14GD+TgPcDxAAlj1Pc9L1ve9H2fV93wJL9f3-QDgOQMDgAgqDkBwuCKMdEMmExR1CIgF0VwJEggA

πŸ’» 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.

Metadata

Metadata

Assignees

No one assigned

    Labels

    Not a DefectThis behavior is one of several equally-correct options

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions