microsoft / microsoft/TypeScript

Way to detect whether a callable has type parameters

Aperta
#61,838 0 commenti 1 reazione 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

conditional type detect callable type parameter

### ✅ Viability Checklist

- [x] This wouldn't be a breaking change in existing TypeScript/JavaScript code
- [x] This wouldn't change the runtime behavior of existing JavaScript code
- [x] This could be implemented without emitting different JS based on the types of the expressions
- [x] This isn't a runtime feature (e.g. library functionality, non-ECMAScript syntax with JavaScript output, new syntax sugar for JS, etc.)
- [x] This isn't a request to add a new utility type: https://github.com/microsoft/TypeScript/wiki/No-New-Utility-Types
- [x] This feature would agree with the rest of our Design Goals: https://github.com/Microsoft/TypeScript/wiki/TypeScript-Design-Goals

### ⭐ Suggestion

I'm looking for some way to detect if a callable type is generic and has type parameters.

LLMs are hallucinating an answer that would be nice if it worked:

```js
type HasTypeParameter any> =
T extends (...args: any[]) => any ? true : false;
```

### 📃 Motivating Example

I have a utility type that wants to map the return value of a callable. However because as far as I know it's not possible to map a type-parametrized callable keeping its parametrization, we prefer to bail out in this case and maintain the original type.

Roughly it looks like

```js
type MappedCallable = HasTypeParameter extends true
? T
: (...args: Parameters) => MappedResult>;
```

### 💻 Use Cases

I can achieve this detection in some case where the type of the callable is incompatible with a mapped callable were its type parameters inferred as `any`.

```js
type LimitedHasTypeArgument any> =
((...args: Parameters) => ReturnType) extends T ? false : true
```

However this doesn't work in some more complex cases (still trying to get a minimal repro).

I cannot think of other workarounds, which is forcing us to forego the result type mapping completely.

Of course having a way to keep the callable type-parametrization in the result mapping would be even better.

The specific use case is to map all callable in the result, including object methods, to callables returning a promise, while maintaining other parts of their signature.

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

L’issue non indica alcun file sorgente, test o punto di ingresso dell’implementazione e afferma che una riproduzione minima è ancora in fase di sviluppo. Inizia riducendo gli esempi di callable generici a un test del compilatore; il lavoro sarebbe considerato completato se esistesse un modo affidabile per rilevare o preservare i parametri di tipo callable, oppure una risoluzione documentata.

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
Da chiarire
Idoneità per principianti
25/100

Ricevi le nuove issue nella tua casella

Un breve riepilogo di issue GitHub adatte ai principianti.