Skip to content

Missing type error for invalid use of #private field #43711

@trusktr

Description

@trusktr

Bug Report

ES #private fields are not source-positional, but scoped to each instance of a class. (That is unfortunate, in my humble opinion). However TypeScript is not detecting this case.

🔎 Search Terms

typescript no type error private field mixin

🕗 Version & Regression Information

4.3-beta (I didn't try earlier)

⏯ Playground Link

Playground link with relevant code

💻 Code

type Ctor<T = {}> = new (...args: any[]) => T

function FooMixin<C extends Ctor>(Base: C) {
  return class Foo extends Base {
    #foo = 123
    test(foo: Foo) {
      console.log(foo.#foo)
    }
  }
}

class Cat extends FooMixin(Object) {}
class Dog extends FooMixin(Object) {}

const c = new Cat
c.test(new Dog) // Should there be a type error here, perhaps?

🙁 Actual behavior

No type error, but only a runtime error.

🙂 Expected behavior

There should perhaps also be a type error, if that's possible.

Mixin docs

The TypeScript Mixin documentation advertises that people can use #private fields with class-factory mixins:

// Mixins may not declare private/protected properties
// however, you can use ES2020 private fields

This may lead people to the try them and they'll have this issue.

Metadata

Metadata

Assignees

No one assigned

    Labels

    Awaiting More FeedbackThis means we'd like to hear from more people who would be helped by this featureSuggestionAn idea for TypeScript

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions