microsoft / microsoft/TypeScript

Type instantiation for type level generic function

Aperta
#62,524 4 commenti 0 reazioni 0 assegnatari Vedi su GitHub

Nessuno ha ancora preso questa issue.

Awaiting More Feedback Suggestion
Lingua principale
Go
Stelle
111k
Fork
14.3k
Merge medio
2g 4h
PR unite (30g)
132

Descrizione

🔍 Search Terms

type instantiation
type level generic function
generic return type

Related: https://github.com/microsoft/TypeScript/issues/40542, https://github.com/microsoft/TypeScript/issues/61133 and maybe https://github.com/microsoft/TypeScript/issues/52035?

Edit: Just found https://github.com/microsoft/TypeScript/issues/55435 and https://github.com/microsoft/TypeScript/issues/40179

✅ Viability Checklist
⭐ Suggestion

#47607 added support for instantiation expressions, but those do not apply at the type level. I understand it'd be ambiguous in some cases (https://github.com/microsoft/TypeScript/pull/47607#issuecomment-1058192563), but in some cases there really isn't a reasonable workaround.

📃 Motivating Example

I actually have a very similar case to the example provided in https://github.com/microsoft/TypeScript/issues/40542

const provideBox = () => {
    const box = <T>(value: T) => ({value});
    return box;
};

type BoxMaker = ReturnType<typeof provideBox>
// type BoxMaker = <T>(value: T) => { value: T; }

type Box1<T> = ReturnType<BoxMaker<T>>;
// Type 'BoxMaker' is not generic.(2315)

const box = undefined as unknown as BoxMaker;

type Box2<T> = ReturnType<typeof box<T>>;
//type Box2<T> = { value: T; }


Compiler Options
{
  "compilerOptions": {
    "strict": true,
    "noImplicitAny": true,
    "strictNullChecks": true,
    "strictFunctionTypes": true,
    "strictPropertyInitialization": true,
    "strictBindCallApply": true,
    "noImplicitThis": true,
    "noImplicitReturns": true,
    "alwaysStrict": true,
    "esModuleInterop": true,
    "declaration": true,
    "target": "ES2017",
    "module": "ESNext",
    "moduleResolution": "node"
  }
}

Playground Link: Provided

💻 Use Cases
  1. What do you want to use this for?

In our case we have a more complex provideBox that accept arguments. We also need to rely on type inference for the generic type of the box function and its return value;

  1. What shortcomings exist with current approaches?

I cannot extract and export a generic type for a return value of a generic function if I don't have direct reference to the function.

  1. What workarounds are you using in the meantime?

The only workaround I found is to use a dummy runtime value to apply the type, and use and instantiation expression on that dummy value (see Box2 above)

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 riproducendo l’esempio motivante nel link Playground fornito, quindi leggi le issue correlate e PR #47607 per comprendere il comportamento esistente delle espressioni di istanziazione e le ambiguità segnalate. Il lavoro è completato quando una forma a livello di tipo può estrarre il tipo restituito generico senza un valore runtime fittizio, producendo il risultato Box2 previsto e preservando il comportamento esistente.

Scritto dal modello di indicizzazione a partire dal testo della issue.

Valutazione

Stack tecnologico
typescript
Ambito
compilers
Tipo di issue
Funzionalità
Difficoltà
5/5
Tempo stimato
Più di una settimana
Stato di attività
Ferma
Chiarezza
Abbastanza chiara
Idoneità per principianti
35/100

Ricevi le nuove issue nella tua casella

Un breve riepilogo di issue GitHub adatte ai principianti.