microsoft / microsoft/TypeScript
Function parameters and return type inference with infer keyword
Nessuno ha ancora preso questa issue.
- Lingua principale
- Go
- Stelle
- 111k
- Fork
- 14.3k
- Merge medio
- 2g 4h
- PR unite (30g)
- 132
Descrizione
I saw a couple of similar issues, but it all provided very poor specification of the problem, so I decided to open my own issue.
Description
The further step in bringing nice fp declarations into TypeScript is adding function parameters and return type inference through infer keyword.
Lets look at some examples:
type F1 = <T extends number>(a: T, b: T) => T
// Now: [number, number]
// Propose: [1, 1]
type R1 = F1 extends ((a: 1, b: infer P) => infer R) ? [P, R] : never
Here we passed parameter a as 1 and expect:
- Type parameter
Tto be inferred as1 - Type
Pto be inferred asT->1 - Type
Rto be inferred asT->1 - Result to be inferred as
[P, R]->[1, 1]
type F2 = <T1 extends AnyTuple, T2>(a: T1, b: T2, c: Reverse<T1>) => [T1, T2]
// Now: [never, unknown, [AnyTuple, unknown]]
// Propose: [[3, 2, 1], unknown, [[1, 2, 3], unknown]]
type R2 = F2 extends (a: [1, 2, 3], b: infer P2, c: infer P1) => infer R ? [P1, P2, R] : never
Here we passed parameter a as [1, 2, 3] and expect:
- Type parameter
T1to be inferred as[1, 2, 3] - Type parameter
T2to be replaced withunknowntype because we provided no info to be able to infer it - Type
P2to be inferred asT2->unknown - Type
P1to be inferred asReverse<T1>->Reverse<[1, 2, 3]>->[3, 2, 1] - Type
Rto be inferred as[T1, T2]->[[1, 2, 3], unknown] - Result to be inferred as
[P1, P2, R]-> `[[3, 2, 1], unknown, [[1, 2, 3], unknown]]
type F3 = <T extends AnyTuple>(a: T, b: T['length']) => typeof b
// Now: never
// Propose: 3
type R3 = F3 extends ((a: [1, 2, 3], b: infer LEN) => any) ? LEN : never
Here we expect:
- Type parameter
Tto be inferred as[1, 2, 3] - Type LEN to be inferred as
T['length']->[1, 2, 3]['length']->3 - Result to be inferred as
LEN->3
Case of wrapping type checkers:
enum string_literal { string_literal = '' }
type StringLiteral<T> = T extends string ? string extends T ? string_literal : T : string_literal
type F4 = <T>(a: StringLiteral<T>, b: T) => T extends '' ? [] : T
// Now: all cases never
// Proposed: ['foobar', 'foobar']
type R4_1 = F4 extends ((a: 'foobar', b: infer P) => infer R) ? [P, R] : never
// Proposed: ['', []]
type R4_2 = F4 extends ((a: '', b: infer P) => infer R) ? [P, R] : never
// Proposed: [string_literal, string]
type R4_3 = F4 extends ((a: string, b: infer P) => infer R) ? [P, R] : never
Use cases
In other similar issues I saw only use cases of function parameters inference, so I'll extend it with return type inference cases.
The first example is a call function:
declare function call<P extends AnyTuple, F extends ((...args: P) => unknown)>(
params: P, func: F
): F extends (...args: P) => infer R ? R : never
// Got: [unknown, unknown]
// Expected: [1, 2]
const a = call([1, 2], <A, B>(a: A, b: B): [A, B] => [a, b])
Also it may be used to infer return types on each step of computing pipe and flow function versions with unlimited parameters count. I'm sure, there're much more cases where this feature may be applied, but I can't remember any more right now.
Util types used
type Empty = readonly []
type AnyTuple = ReadonlyArray<unknown> & { readonly 0: unknown } | Empty
type Tail<T extends AnyTuple> = ((...args: T) => any) extends (head: any, ...tail: infer R) => any
? R extends AnyTuple ? Readonly<R> : never : Empty
type Prepend<T extends AnyTuple, E> = ((e: E, ...args: T) => any) extends
(...args: infer R) => any ? R extends AnyTuple ? Readonly<R> : never : never
type _ReverseWith<T extends AnyTuple, Result extends AnyTuple> = {
0: _ReverseWith<Tail<T>, Prepend<Result, T[0]>>
1: Readonly<Result>
}[T extends Empty ? 1 : 0]
type Reverse<T extends AnyTuple> = _ReverseWith<T, Empty>
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 esaminando gli esempi di tipi condizionali e i risultati attuali e proposti documentati nell’issue. Non sono indicati file di implementazione, test o punti di ingresso, quindi individuare il sottosistema pertinente per l’inferenza dei tipi e definire test compatibili fa parte del lavoro. Il lavoro è completo quando i casi proposti di inferenza dei tipi dei parametri e del valore restituito producono i risultati indicati.
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
- 25/100