microsoft / microsoft/TypeScript

Missing type error for invalid use of #private field

Open
#43,711 2 comments 1 reaction 0 assignees View on GitHub

Nobody has claimed this yet.

Awaiting More Feedback Suggestion
Dominant language
Go
Stars
111k
Forks
14.3k
Avg merge
2d 4h
Merged PRs (30d)
132

Description

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.

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start with the linked TypeScript Playground and reproduce the mixin example involving #private fields, FooMixin, Cat, and Dog. Read the type-checking behavior around private-field identity and determine what diagnostic, if any, should be produced for c.test(new Dog); done requires an agreed semantic and corresponding regression coverage.

Written by the indexing model from the issue text.

Assessment

Tech stack
javascript, typescript
Domain
compilers
Issue type
Bug
Difficulty
5/5
Estimated time
Over a week
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
30/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.