Conversation
## Context - Principle: AnyAsyncFunction must constrain Promise-returning functions without erasing their callable signatures. - Why: Signature-preserving examples and discriminating type boundaries keep the public constraint safe to copy. ## Key Deltas - AnyAsyncFunction documentation: zero-argument invocation after signature erasure -> generic registration that retains parameters and return type; why: prevent examples from implying unsafe calls. Key refs: source/basic.d.ts:62 - AnyAsyncFunction type tests: zero-argument async and sync pair -> required, never, structural Promise, and synchronous boundaries; why: distinguish the accepted structural contract without changing the type. Key refs: test-d/async-function.ts:13 ## Verification - Result: passed
| @category Basic | ||
| */ | ||
| // eslint-disable-next-line @typescript-eslint/no-explicit-any | ||
| export type AnyFunction = (...arguments_: readonly any[]) => any; |
Owner
There was a problem hiding this comment.
Suggested change
| export type AnyFunction = (...arguments_: readonly any[]) => any; | |
| export type AnyFunction = (...arguments_: readonly any[]) => unknown; |
Author
There was a problem hiding this comment.
Updated AnyFunction to return unknown as suggested.
|
|
||
| expectAssignable<AnyFunction>((value: never) => value); | ||
| expectAssignable<AnyFunction>(async () => 'value'); | ||
| expectAssignable<AnyFunction>(() => 'value'); |
Owner
There was a problem hiding this comment.
I would add a few more tests:
// `Arguments` is enforced.
expectError(asyncFunction());
expectError(asyncFunction(123));
// `ReturnValue` is enforced.
expectNotAssignable<AsyncFunction<[value: string], number>>(
async (value: string) => value,
);
// Defaults accept arbitrary arguments.
expectType<Promise<unknown>>(defaultAsyncFunction('value', 123));
// Erased functions remain callable with an unknown result.
declare const anyFunction: AnyFunction;
expectType<unknown>(anyFunction('value', 123));
declare const erasedAsyncFunction: AnyAsyncFunction;
expectType<Promise<unknown>>(erasedAsyncFunction('value', 123));
// Ordinary required parameters are supported.
expectAssignable<AnyFunction>((value: string) => value);
Author
There was a problem hiding this comment.
Added these cases, including direct invalid-call checks for the argument constraints. The full npm test suite passes locally.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Closes #121
This adds the three connected Basic function types from the final issue specification:
AsyncFunction<Arguments, ReturnValue>for a reusable asynchronous signature;AnyAsyncFunctionfor an erased asynchronous function constraint;AnyFunctionfor an erased sync-or-async function constraint.The definitions follow the names, defaults, and
anyedge-case decision discussed in #121. They live alongside the existing class and constructor types insource/basic.d.ts, with README entries and examples.Tests cover generic arguments and return values, defaults,
any, rejection of synchronous functions byAnyAsyncFunction, acceptance of synchronous and asynchronous functions byAnyFunction, and aneverparameter.Validation:
npx tsd --files test-d/async-function.tsnpm run test:tsdnpm run test:tscnpm run test:xonpm run test:linterCodex assisted with checking the type edge cases, naming, documentation, examples, and test coverage against the upstream discussion and repository conventions.