microsoft / microsoft/TypeScript
should error on assignment that shadows a prototype method
Nessuno ha ancora preso questa issue.
- Lingua principale
- Go
- Stelle
- 111k
- Fork
- 14.4k
- Merge medio
- 1g 19h
- PR unite (30g)
- 117
Descrizione
🔎 Search Terms
own property, shadow, prototype, method, field, inheritance
🕗 Version & Regression Information
- This is the behavior in every version I tried, and I reviewed
⏯ Playground Link
💻 Code
class Foo {
constructor() {
this.prototypeMethod = () => {
console.log('shadows prototype method');
}
}
prototypeMethod() {
console.log('method on prototype');
}
}
🙁 Actual behavior
no error
🙂 Expected behavior
error - something like "Type 'Foo' has no instance property named 'prototypeMethod'. If you meant to reassign the method 'prototypeMethod', it must be a function-valued instance property instead of a prototype method.".
I expect an error here because the code is effectively equivalent to
class Foo {
prototypeMethod = () => {
console.log('shadows prototype method');
};
prototypeMethod() {
console.log('method on prototype');
}
}
which is a TS error (see https://github.com/microsoft/TypeScript/issues/13141).
Furthermore, it breaks error reporting in subclasses. The following code correctly reports a TS error due to the extended class not being able to override an instance property with a method:
(playground)
class Foo {
instanceProperty = () => {
console.log('function-valued instance property');
}
}
class Bar extends Foo {
// TS ERROR: Class 'Foo' defines instance member property 'instanceProperty', but extended class 'Bar' defines it as instance member function.(2425)
instanceProperty() {
console.log('overridden with method')
}
}
const b = new Bar();
b.instanceProperty(); // prints 'function-valued instance property'
However, this does not (playground)
class Foo {
constructor() {
this.method = () => {
console.log('function-valued instance property')
}
}
method() {
console.log('method');
}
}
class Bar extends Foo {
// no error here :(
method() {
console.log('overridden with method')
}
}
const b = new Bar();
b.method(); // prints 'function-valued instance property'
Additional information about the issue
inspired by https://github.com/typescript-eslint/typescript-eslint/issues/10427
cc @miguel-leon
Guida per i contributori
Apri la guida per i contributori
Come iniziare
- Leggi tutta la issue e poi la guida ai contributi del progetto.
- Commenta sulla issue per dire che te ne occupi tu — evita che due persone facciano lo stesso lavoro.
- Fai un fork del repository e lavora su un branch.
- Apri una pull request che faccia riferimento al numero della issue.
Direzione di ricerca
Riproduci gli esempi di assegnazione nel costruttore e di sottoclasse nel TypeScript Playground collegato, quindi confrontali con il caso equivalente di una proprietà di istanza il cui valore è una funzione. Traccia i controlli del compilatore sui membri della classe e sull’ereditarietà; il lavoro è completato quando le assegnazioni che nascondono i metodi del prototipo producono la diagnosi prevista e gli override delle sottoclassi vengono verificati in modo coerente.
Scritto dal modello di indicizzazione a partire dal testo della issue.
Valutazione
- Stack tecnologico
- javascript, typescript
- Ambito
- compilers
- Tipo di issue
- Bug
- Difficoltà
- 4/5
- Tempo stimato
- 3-5 giorni
- Stato di attività
- Tranquilla
- Chiarezza
- Abbastanza chiara
- Idoneità per principianti
- 45/100