microsoft / microsoft/TypeScript
Non‑`void` returning assertion functions
Nessuno ha ancora preso questa issue.
- Lingua principale
- Go
- Stelle
- 111k
- Fork
- 14.3k
- Merge medio
- 2g 4h
- PR unite (30g)
- 132
Descrizione
Search Terms
- assertion function non-void return type
- assertion function non-void return
- assertion function generic return
- assertion function custom return
Suggestion
A way to type a function that is both an assertion function and returning a value that is not void.
Use Cases
This is necessary to correctly type Jest’s expect(…) matchers, which have a generic return type of R.
Examples
declare const expect: <T>(actual: T): JestMatchers<T>;
type JestMatchers<T> = JestMatchersShape<
Matchers<void, T>,
Matchers<
Promise<void>,
T extends PromiseLike<infer U>
? U
: Exclude<T, PromiseLike<any>>
>
>;
type JestMatchersShape<TNonPromise extends {} = {}, TPromise extends {} = {}> = {
resolves: AndNot<TPromise>;
rejects: AndNot<TPromise>;
} & AndNot<TNonPromise>
type AndNot<T> = T & { not: T };
interface Matchers<R, T = {}> {
toBe<E>(expected: E): R & (asserts T is E);
}
declare const foo: unknown;
expect(foo).toBe("foo");
foo; // $ExpectType "foo"
// Some typings for engine262's Completion Record handling:
type UnwrapNormalCompletion<T>
= unknown extends T ? Value | undefined
: T extends NormalCompletion<infer V>
? (unknown extends V ? Value | undefined : V)
: T extends Completion ? never
: T;
/** @see https://tc39.es/ecma262/#sec-returnifabrupt */
export declare function ReturnIfAbrupt<T>(completion: T):
(UnwrapNormalCompletion<T>)
& (asserts completion is UnwrapNormalCompletion<T>);
/** @see https://tc39.es/ecma262/#sec-returnifabrupt-shorthands */
export { ReturnIfAbrupt as Q };
/**
* The type signature is the same, but `AssertNormalCompletion` causes an error to be thrown at runtime
* if `argument` is an AbruptCompletion, whereas `ReturnIfAbrupt` gets replaced with code that causes
* the caller to return the AbruptCompletion by engine262's build system:
*
* @see https://tc39.es/ecma262/#sec-returnifabrupt
* @see https://tc39.es/ecma262/#sec-returnifabrupt-shorthands
*/
declare function AssertNormalCompletion<T>(completion: T):
(UnwrapNormalCompletion<T>)
& (asserts completion is UnwrapNormalCompletion<T>);
export { AssertNormalCompletion as X };
Checklist
My suggestion meets these guidelines:
- This wouldn't be a breaking change in existing TypeScript/JavaScript code
- This wouldn't change the runtime behavior of existing JavaScript code
- This could be implemented without emitting different JS based on the types of the expressions
- This isn't a runtime feature (e.g. library functionality, non-ECMAScript syntax with JavaScript output, etc.)
- This feature would agree with the rest of TypeScript's Design Goals.
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
Non vengono indicati file di implementazione né test. Inizia esaminando la gestione esistente delle funzioni di asserzione da parte di TypeScript e gli esempi per i matcher di Jest e i completion records. Il lavoro è completo quando vengono supportate funzioni di asserzione con valori restituiti generici non-void, preservando al contempo il restringimento dei tipi dimostrato.
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à
- Tranquilla
- Chiarezza
- Abbastanza chiara
- Idoneità per principianti
- 30/100