microsoft / microsoft/TypeScript
Different behavior when declare property or method in a class about covariance
Nessuno ha ancora preso questa issue.
- Lingua principale
- Go
- Stelle
- 111k
- Fork
- 14.3k
- Merge medio
- 2g 4h
- PR unite (30g)
- 132
Descrizione
TypeScript Version: 4.1.2
Search Terms: In ts doc - type compatibility, it seems like not mentioned about this.
Code
class M1 {
c: number = 1;
}
export class M2 extends M1 {
a: number = 1;
}
type Func<M extends M1> = (m: M) => number;
interface IFunc<M extends M1> {
func1?: Func<M>,
func2: Func<M>,
}
class R1<M extends M1 = M1> implements IFunc<M> {
// when delete this property, it will be no error.
public func1 = (m: M) => {
return m.c
}
public func2(m: M) {
return m.c
};
}
type IRConstructor<M extends M1, R extends R1<M>> = new () => R;
export class R2 extends R1<M2> {
}
type A = IRConstructor<M1, R1>;
let a1: A = R2; // error: Type 'typeof R2' is not assignable to type 'IRConstructor<M1, R1<M1>>'.
console.log(a1);
Expected behavior:
Should declare a property or a method in class has same behavior? I'm not sure...
Actually I want to know how can I make these code without error when I have to use functional property.
Actual behavior:
When you just decalre a method which is compatible with IFunc<M1>, it will be fine. But when you declare a functional property which is compatible with IFunc<M1>, it will be an error.
Playground Link:
Or, you can see playground
Related Issues:
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
Inizia con la riproduzione collegata in TypeScript Playground e con la sezione del manuale «Type Compatibility» a cui si fa riferimento nell'issue. Confronta il comportamento segnalato della classe generica per le forme con proprietà funzionale e con metodo, quindi stabilisci se la differenza è intenzionale; il lavoro è completato quando è stato definito un comportamento atteso ed esiste una copertura corrispondente nel compilatore o nella documentazione.
Scritto dal modello di indicizzazione a partire dal testo della issue.
Valutazione
- Stack tecnologico
- typescript
- Ambito
- compilers
- Tipo di issue
- Bug
- Difficoltà
- 4/5
- Tempo stimato
- 3-5 giorni
- Stato di attività
- Ferma
- Chiarezza
- Da chiarire
- Idoneità per principianti
- 35/100