Skip to content

Commit

Permalink
Typescript update
Browse files Browse the repository at this point in the history
  • Loading branch information
fabiancook committed Jul 17, 2023
1 parent fdc1528 commit c2c6a6b
Show file tree
Hide file tree
Showing 5 changed files with 35 additions and 29 deletions.
4 changes: 2 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
".": "./dist/index.js"
},
"scripts": {
"build": "tsc --declaration && npm run tslint && node correct-import-extensions.js",
"build": "tsc --declaration && node correct-import-extensions.js",
"watch": "tsc -w",
"tslint": "tslint -c tslint.json -p tsconfig.json",
"prepublishOnly": "npm run build",
Expand All @@ -34,6 +34,6 @@
"esm": "^3.2.25",
"filehound": "^1.17.5",
"tslint": "^6.1.3",
"typescript": "^4.5.3"
"typescript": "^5.1.6"
}
}
3 changes: 2 additions & 1 deletion src/engine/context.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,8 @@ import { ExpectedAsyncOperationError } from "../operations/async";
export interface InputFunction<T = unknown, O = unknown> {
(input: T): O;
}
export type InputOperationsArray<T = unknown, O = unknown> = [InputFunction<T, O>, ...InputFunction<T, O>[]];
type InputOperation<T = unknown, O = unknown> = AsyncOperation<T, O> | SyncOperation<T, O> | InputFunction<T, O>;
export type InputOperationsArray<T = unknown, O = unknown> = [InputOperation<T, O>, ...InputOperation<T, O>[]];

type OperationReturnType<V> =
V extends Iterable<infer Z> ?
Expand Down
28 changes: 15 additions & 13 deletions src/tests/tc39/async.ts
Original file line number Diff line number Diff line change
Expand Up @@ -34,21 +34,23 @@ export interface TC39AsyncIterableHelpersObject<T> extends AsyncIterable<T> {
find?(filterFn: FilterFn<T>): Promise<T | undefined>;
}

export interface TC39AsyncIteratorHelpersFn<O extends InputOperationsArray, T> {
(...operations: O): Omit<IterableEngineContext<O, never | number>, "instance"> & { instance(input: AsyncIterable<T> | Iterable<T>): TC39AsyncIterableHelpersObject<T> };
export interface TC39AsyncIteratorHelpersFn<T> {
<O extends InputOperationsArray>(...operations: O): Omit<IterableEngineContext<O, never | number>, "instance"> & { instance(input: AsyncIterable<T> | Iterable<T>): TC39AsyncIterableHelpersObject<T> };
}

export interface AssertTC39AsyncIteratorHelpersFn<O extends InputOperationsArray = InputOperationsArray, T = unknown> {
(test: unknown): asserts test is TC39AsyncIteratorHelpersFn<O, T>;
(test: unknown): asserts test is TC39AsyncIteratorHelpersFn<T>;
}

export async function assertTC39AsyncIteratorHelpersObject<O extends InputOperationsArray, T>(test: unknown): Promise<AssertTC39AsyncIteratorHelpersFn<O, T>> {
export async function assertTC39AsyncIteratorHelpersObject<O extends InputOperationsArray, T>(input: unknown): Promise<AssertTC39AsyncIteratorHelpersFn<O, T>> {
let error: unknown;
try {
function assertFunction(value: unknown): asserts value is (...operations: unknown[]) => ReturnType<TC39AsyncIteratorHelpersFn<O, unknown>> {
if (typeof test !== "function") throw new Error();
type HelperFn = <T = unknown, O = unknown, A extends InputOperationsArray<T, O> = InputOperationsArray<T, O>>(...operations: A) => ReturnType<TC39AsyncIteratorHelpersFn<T>>;
function assertFunction(value: unknown): asserts value is HelperFn {
if (typeof value !== "function") throw new Error();
}
assertFunction(test);
assertFunction(input);
const test: HelperFn = input;

function* naturals() {
let i = 0;
Expand Down Expand Up @@ -146,24 +148,24 @@ export async function assertTC39AsyncIteratorHelpersObject<O extends InputOperat
await getThrownResult(forEachOps.instance(naturals()));
ok(forEachLog.join(", ") === "1, 2, 3"); // "1, 2, 3"

const someOps = test(take(4), some(v => v > 1));
const someOps = test<number>(take(4), some(v => v > 1));
ok((await getThrownResult(someOps.instance(naturals()))) === true);
const someOpsOneTrue = test(take(4), some(v => v === 1));
ok((await getThrownResult(someOpsOneTrue.instance(naturals()))) === true);

const everyOverOps = test(drop(1), take(4), every(v => v > 1));
const everyOverOps = test<number>(drop(1), take(4), every(v => v > 1));
ok(await getThrownResult(everyOverOps.instance(naturals())) === false); // false, first value is 1
const everyOverEqualOps = test(drop(1), take(4), every(v => v >= 1));
const everyOverEqualOps = test<number>(drop(1), take(4), every(v => v >= 1));
ok(await getThrownResult(everyOverEqualOps.instance(naturals())) === true);

const findOps = test(find((value: number) => value > 1));
const findOps = test<number>(find((value: number) => value > 1));
ok(await getThrownResult(findOps.instance(naturals())) === 2);
} catch (caught) {
error = caught;
}

return (inner: unknown) => {
if (inner !== test) throw new Error("Unexpected value");
return (inner): asserts inner is TC39AsyncIteratorHelpersFn<T> => {
if (inner !== input) throw new Error("Unexpected value");
if (error) throw error;
};

Expand Down
21 changes: 12 additions & 9 deletions src/tests/tc39/sync.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ import {
toArray
} from "../../operations/sync";
import { isIterable } from "../../async-like";
import { TC39AsyncIteratorHelpersFn } from "./async";

export interface TC39IterableHelpersObject<T> extends Iterable<T> {
map?<O>(mapperFn: MapFn<T, O>): TC39IterableHelpersObject<O>;
Expand All @@ -33,12 +34,14 @@ export interface TC39IterableHelpersObject<T> extends Iterable<T> {
find?(filterFn: FilterFn<T>): T | undefined;
}

export function assertTC39IteratorHelpersObject<O extends InputOperationsArray, T>(test: unknown): asserts test is (...operations: O) => Omit<IterableEngineContext<O, never | number>, "instance"> & { instance(): TC39IterableHelpersObject<T> } {
export function assertTC39IteratorHelpersObject<O extends InputOperationsArray, T>(input: unknown): asserts input is (...operations: O) => Omit<IterableEngineContext<O, never | number>, "instance"> & { instance(): TC39IterableHelpersObject<T> } {

function assertFunction(value: unknown): asserts value is (...operations: unknown[]) => Omit<IterableEngineContext<unknown[], never | number>, "instance"> & { instance<T>(input: Iterable<T>): TC39IterableHelpersObject<T> } {
if (typeof test !== "function") throw new Error();
type HelperFn = <T = unknown, O = unknown, A extends InputOperationsArray<T, O> = InputOperationsArray<T, O>>(...operations: A) => (Omit<IterableEngineContext<A, never | number>, "instance"> & { instance<T>(input: Iterable<T>): TC39IterableHelpersObject<T> });
function assertFunction(value: unknown): asserts value is HelperFn {
if (typeof value !== "function") throw new Error();
}
assertFunction(test);
assertFunction(input);
const test: HelperFn = input;

function* naturals() {
let i = 0;
Expand Down Expand Up @@ -136,21 +139,21 @@ export function assertTC39IteratorHelpersObject<O extends InputOperationsArray,
ok(naturalsArrayResultDone.done);

const forEachLog: unknown[] = [];
const forEachOps = test(drop(1), take(3), forEach((value) => forEachLog.push(value)));
const forEachOps = test<number>(drop(1), take(3), forEach((value) => forEachLog.push(value)));
getThrownResult(forEachOps.instance(naturals()));
ok(forEachLog.join(", ") === "1, 2, 3"); // "1, 2, 3"

const someOps = test(take(4), some(v => v > 1));
const someOps = test<number>(take(4), some(v => v > 1));
ok(getThrownResult(someOps.instance(naturals())) === true);
const someOpsOneTrue = test(take(4), some(v => v === 1));
ok(getThrownResult(someOpsOneTrue.instance(naturals())) === true);

const everyOverOps = test(drop(1), take(4), every(v => v > 1));
const everyOverOps = test<number>(drop(1), take(4), every(v => v > 1));
ok(getThrownResult(everyOverOps.instance(naturals())) === false); // false, first value is 1
const everyOverEqualOps = test(drop(1), take(4), every(v => v >= 1));
const everyOverEqualOps = test<number>(drop(1), take(4), every(v => v >= 1));
ok(getThrownResult(everyOverEqualOps.instance(naturals())) === true);

const findOps = test(find((value: number) => value > 1));
const findOps = test<number>(find((value: number) => value > 1));
ok(getThrownResult(findOps.instance(naturals())) === 2);

function getThrownResult(iterable: unknown) {
Expand Down
8 changes: 4 additions & 4 deletions yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -819,10 +819,10 @@ tsutils@^2.29.0:
dependencies:
tslib "^1.8.1"

typescript@^4.5.3:
version "4.5.3"
resolved "https://registry.npmjs.org/typescript/-/typescript-4.5.3.tgz#afaa858e68c7103317d89eb90c5d8906268d353c"
integrity sha512-eVYaEHALSt+s9LbvgEv4Ef+Tdq7hBiIZgii12xXJnukryt3pMgJf6aKhoCZ3FWQsu6sydEnkg11fYXLzhLBjeQ==
typescript@^5.1.6:
version "5.1.6"
resolved "https://registry.yarnpkg.com/typescript/-/typescript-5.1.6.tgz#02f8ac202b6dad2c0dd5e0913745b47a37998274"
integrity sha512-zaWCozRZ6DLEWAWFrVDz1H6FVXzUSfTy5FUMWsQlU8Ym5JP9eO4xkTIROFCQvhQf61z6O/G6ugw3SgAnvvm+HA==

unit-compare@^1.0.1:
version "1.0.1"
Expand Down

0 comments on commit c2c6a6b

Please sign in to comment.