quidproquo-core
Advanced tools
Comparing version 0.0.168 to 0.0.169
@@ -18,21 +18,32 @@ import { ErrorTypeEnum } from './ErrorTypeEnum'; | ||
/** | ||
* Represents the return type of an `AskResponse`. | ||
* Utility Type: ExtractGeneratorReturnType | ||
* | ||
* When extracting the return type of a generator (or iterator) in TypeScript, | ||
* the inferred type is a union of all possible `yield` values combined with the `return` value. | ||
* This utility type is designed to extract the return type of a Generator without | ||
* conflating it with the types of values it may yield. Generators in TypeScript are | ||
* annotated as Generator<YieldType, ReturnType, NextType>, where YieldType is the | ||
* type of values yielded by the generator, ReturnType is the type the generator returns | ||
* when it's done, and NextType is the type of the value passed to the generator's `next` method. | ||
* | ||
* For our generators, they typically yield `Action<any>` and return some distinct type. | ||
* However, TypeScript doesn't provide a direct utility to exclusively extract the iterator's return type. | ||
* By leveraging TypeScript's conditional types and the `infer` keyword, ExtractGeneratorReturnType | ||
* can precisely capture and extract just the ReturnType of any given Generator, providing a clean, | ||
* type-safe way to understand what a generator ultimately returns upon completion. | ||
* | ||
* To navigate this, we begin by extracting the union type of both yielded and returned values with | ||
* `ReturnType<T['next']>['value']`. This typically results in a type like `Action<any> | SomeOtherType`. | ||
* Usage of `infer` in Conditional Types: | ||
* The `infer` keyword is used here to declare a type variable R within the conditional type | ||
* expression. TypeScript will then infer the type R as the generator's ReturnType when the | ||
* condition `T extends Generator<any, infer R, any>` is met. This inference is based on the | ||
* actual type passed to ExtractGeneratorReturnType, allowing us to "pull out" the ReturnType | ||
* from within the Generator type. | ||
* | ||
* In order to solely retrieve the desired return type (i.e., `SomeOtherType`), we use the `Exclude` | ||
* utility type from TypeScript to exclude `Action<any>` from this union. This leaves us only with | ||
* the intended return type. | ||
* This approach is particularly useful because it directly targets the ReturnType without | ||
* having to manually sift through or exclude the types of values that might be yielded by | ||
* the generator, which can often be diverse and not relevant to the final return type we're | ||
* interested in capturing. | ||
* | ||
* With this approach, `AskResponseReturnType` precisely represents the type that the generator | ||
* returns, and not any intermediary `yield` values. | ||
* The ExtractGeneratorReturnType utility type simplifies the extraction process and ensures | ||
* that types dependent on the return value of generators are accurately typed, enhancing | ||
* code readability and maintainability. | ||
*/ | ||
export type AskResponseReturnType<T extends AskResponse<any>> = Exclude<ReturnType<T['next']>['value'], Action<any>>; | ||
export type ExtractGeneratorReturnType<T extends Generator> = T extends Generator<any, infer R, any> ? R : never; | ||
export type AskResponseReturnType<T extends AskResponse<any>> = ExtractGeneratorReturnType<T>; | ||
export interface ActionHistory<T = any> { | ||
@@ -39,0 +50,0 @@ act: Action<T>; |
{ | ||
"name": "quidproquo-core", | ||
"version": "0.0.168", | ||
"version": "0.0.169", | ||
"description": "", | ||
@@ -28,5 +28,5 @@ "main": "./lib/commonjs/index.js", | ||
"devDependencies": { | ||
"quidproquo-tsconfig": "0.0.168", | ||
"quidproquo-tsconfig": "0.0.169", | ||
"typescript": "^4.9.3" | ||
} | ||
} |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
273972
5348