microsoft / microsoft/TypeScript
Missing type error for invalid use of #private field
Nobody has claimed this yet.
- 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
First steps
- Read the whole issue, then the project's contributing guide.
- Comment on the issue to say you are picking it up — it saves two people doing the same work.
- Fork the repository and make your change on a branch.
- 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