microsoft / microsoft/TypeScript

Arguments cease to be assignable when parameter is broadened via conditional

Aperta
#52,310 5 commenti 0 reazioni 0 assegnatari Vedi su GitHub

Nessuno ha ancora preso questa issue.

Bug Domain: Conditional Types Help Wanted
Lingua principale
Go
Stelle
111k
Fork
14.4k
Merge medio
1g 19h
PR unite (30g)
117

Descrizione

Bug Report

🔎 Search Terms

conditional parameter assignable broader

🕗 Version & Regression Information
  • The error messages changed to be a bit less informative over time, but the core error was there in all the versions I tested, and I reviewed the FAQ for entries about conditional types and parameters.
  • Nightly version at time of testing: v5.0.0-dev.20230119
⏯ Playground Link

Playground link with relevant code

💻 Code
type AttributeMap = {
    Animal: { swims: boolean; lifespan: number; };
    Robot: { swims: boolean; serial: string; };
}
type Mover = keyof AttributeMap;
type Mountain = { name: string; elevation: number;}
type MountainAttribute = keyof Mountain;
type MoverOrMountain = Mover | 'Mountain';
type BroaderAttributeName<M extends MoverOrMountain> = (
    M extends Mover ?
    keyof AttributeMap[M] :
    MountainAttribute
) & string;
declare function actOnAttribute<M extends Mover>(
    mover : M,
    attr : keyof AttributeMap[M] & string,
) : void ;
declare function actOnAttributeBroader<M extends MoverOrMountain>(
    moverOrMountain : M,
    attr : BroaderAttributeName<M>,
) : void ;
//Sometimes-possible workaround:
interface ExtendedAttributeMap extends AttributeMap {
    Mountain: { name: string; elevation: number };
}
declare function actOnAttributeBroaderWorkaround<
    M extends keyof ExtendedAttributeMap,
>(
    moverOrMountain : M,
    attr : keyof ExtendedAttributeMap[M] & string,
) : void ;
export const demoFunction = function<M extends Mover> (
    mover : M,
    attr : keyof AttributeMap[M] & string,
) {
    actOnAttribute(mover, attr); //works, as expected
    actOnAttributeBroader(mover, attr); //Err: 2nd arg not assignable
    actOnAttributeBroader<M>(mover, attr); //specifying M doesn't help
    actOnAttributeBroaderWorkaround(mover, attr); //works, as expected
}

Note: Dropping instances of ‘& string’ just results in a more complicated error message; it doesn’t change the core issue.

🙁 Actual behavior

actOnAttributeBroader accepts a superset of the valid parameters of actOnAttribute.
In this calling context, these two versions of the called function are equivalent, since all types in that context are limited to the more narrow set accepted by actOnAttribute. However, the TypeScript compiler doesn't even try to figure out that the one conditional type of this example code simplifies to the true branch in the context where the error is triggered, so there isn't actually a need to trigger an error. It triggers an error.

🙂 Expected behavior

Making a reusable function more broadly reusable by accepting a broader set of parameter types should (a) possibly allow some calls that wouldn't work before the broadening to work, and (b) not cause any calls that were working fine before the broadening to suddenly start throwing Typescript compilation failures. Generic, reusable code should be considered a good thing and TypeScript should support that goal (as it seems to in the marketing that convinces technical executives to trigger conversions) rather than fight against it (as it does in practice with in-practice design choices and implementation defects, where this issue is an example).

Where Typescript correctly validates all parameters to a call to actOnAttribute and has enough information to determine which side of the conditional will apply (as it does here), it should do so and simplify the broader possibility of the reusable function down to the simpler original version where it can correctly validate types. There should be no error.

Guida per i contributori

Apri la guida per i contributori

Come iniziare

  1. Leggi tutta la issue e poi la guida ai contributi del progetto.
  2. Commenta sulla issue per dire che te ne occupi tu — evita che due persone facciano lo stesso lavoro.
  3. Fai un fork del repository e lavora su un branch.
  4. Apri una pull request che faccia riferimento al numero della issue.

Direzione di ricerca

Inizia dalla riproduzione collegata in TypeScript Playground e confronta actOnAttribute con actOnAttributeBroader in demoFunction. Traccia il modo in cui il compilatore verifica il tipo condizionale BroaderAttributeName rispetto al parametro generico attr. Il lavoro è completato quando la chiamata più ampia viene compilata senza errori, mentre le chiamate non valide esistenti continuano a essere rifiutate, con un test di regressione per l’esempio segnalato.

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
Abbastanza chiara
Idoneità per principianti
38/100

Ricevi le nuove issue nella tua casella

Un breve riepilogo di issue GitHub adatte ai principianti.