Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Getters in mixins get serialized to property accessors #58790

Open
KonnorRogers opened this issue Jun 6, 2024 · 3 comments
Open

Getters in mixins get serialized to property accessors #58790

KonnorRogers opened this issue Jun 6, 2024 · 3 comments
Labels
Help Wanted You can do this Possible Improvement The current behavior isn't wrong, but it's possible to see that it might be better in some cases
Milestone

Comments

@KonnorRogers
Copy link

KonnorRogers commented Jun 6, 2024

πŸ”Ž Search Terms

Getters, mixins, classes

πŸ•— Version & Regression Information

  • This is the behavior in every version I tried, and I reviewed the FAQ

⏯ Playground Link

https://tsplay.dev/mA0RkN

πŸ’» Code

function mixin<T extends {new (...args: any[]): {}}> (superclass: T) {
   return class extends superclass {
      get validationTarget (): HTMLElement {
         return document.createElement("input")
      }
   }
}
class BaseClass {
   get validationTarget (): HTMLElement {
      return document.createElement("div")
   }
}

class MyClass extends mixin(BaseClass) {
  get validationTarget (): HTMLElement {
   return document.createElement("select")
  }
}
Output
"use strict";
function mixin(superclass) {
    return class extends superclass {
        get validationTarget() {
            return document.createElement("input");
        }
    };
}
class BaseClass {
    get validationTarget() {
        return document.createElement("div");
    }
}
class MyClass extends mixin(BaseClass) {
    get validationTarget() {
        return document.createElement("select");
    }
}
Compiler Options
{
  "compilerOptions": {
    "strict": true,
    "noImplicitAny": true,
    "strictNullChecks": true,
    "strictFunctionTypes": true,
    "strictPropertyInitialization": true,
    "strictBindCallApply": true,
    "noImplicitThis": true,
    "noImplicitReturns": true,
    "alwaysStrict": true,
    "esModuleInterop": true,
    "declaration": true,
    "target": "ES2017",
    "jsx": "react",
    "module": "ESNext",
    "moduleResolution": "node"
  }
}

Playground Link: Provided

πŸ™ Actual behavior

The mixin creates a property accessor instead of a getter

πŸ™‚ Expected behavior

The mixin should create a getter and not a property accessor

Additional information about the issue

Seems related:

#54879 (comment)

Duplicate of: #44938

@Andarist
Copy link
Contributor

Andarist commented Jun 7, 2024

the problem is that createUnionOrIntersectionProperty always produces a symbol with SymbolFlags.Property on it, but at the very least when all combined properties are accessors it should have proper SymbolFlags.Accessor

that combined property is created here: https://github.dev/microsoft/TypeScript/blob/f5238c328e553df2c0af868173dfd3789d77205f/src/compiler/checker.ts#L15089

@RyanCavanaugh RyanCavanaugh added Help Wanted You can do this Possible Improvement The current behavior isn't wrong, but it's possible to see that it might be better in some cases labels Jun 13, 2024
@RyanCavanaugh RyanCavanaugh added this to the Backlog milestone Jun 13, 2024
@KonnorRogers
Copy link
Author

Also of note, this does not affect static getters, only instance getters.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Help Wanted You can do this Possible Improvement The current behavior isn't wrong, but it's possible to see that it might be better in some cases
Projects
None yet
Development

No branches or pull requests

3 participants