From 1a3cf1d783ca049eb16309c3996e09b71c92fc29 Mon Sep 17 00:00:00 2001 From: ASDAlexander77 Date: Sat, 21 Nov 2026 09:58:57 +0000 Subject: [PATCH 1/3] slice() with no arguments, Promise.resolve()/reject() with none, toLocaleString(locales, options) on arrays Library gaps TypeScript's compiler tests still hit: - String.prototype.slice: indexStart is optional and defaults to 0, so slice() copies the string (controlFlowWithIncompleteTypes). - Promise.resolve() gives a settled promise of nothing; Promise.reject() throws undefined, boxed in an any since undefined itself can't be thrown (modulePreserveTopLevelAwait1, asyncFunctionContextuallyTypedReturns). - Arrays (new), TypedArray and ConvertingTypedArray toLocaleString take locales and options (ToLocaleStringOptions), not applied: there is no locale data (arrayToLocaleStringES2015/2020). Co-Authored-By: Claude Opus 5.5 --- src/generics/lib.generics.ts | 41 ++++++++++++++++++++++++++++++----- src/lib.d.ts | 4 ++-- src/lib.ts | 2 +- tests/array_toLocaleString.ts | 24 ++++++++++++++++++++ tests/promise_no_args.ts | 32 +++++++++++++++++++++++++++ tests/string_slice_no_args.ts | 18 +++++++++++++++ 6 files changed, 113 insertions(+), 8 deletions(-) create mode 100644 tests/array_toLocaleString.ts create mode 100644 tests/promise_no_args.ts create mode 100644 tests/string_slice_no_args.ts diff --git a/src/generics/lib.generics.ts b/src/generics/lib.generics.ts index 2ec170f..089df96 100644 --- a/src/generics/lib.generics.ts +++ b/src/generics/lib.generics.ts @@ -16,6 +16,22 @@ type OmitThisParameterType = T extends (this: never, ...args: infer A) => inf type ThisType = T; +// The options toLocaleString takes (Intl's number and date-time options, the ones in common use). +// Taken so such calls compile; there is no locale data to apply them with. +interface ToLocaleStringOptions { + localeMatcher?: string; + style?: string; + currency?: string; + currencyDisplay?: string; + unit?: string; + useGrouping?: boolean; + minimumIntegerDigits?: number; + minimumFractionDigits?: number; + maximumFractionDigits?: number; + timeZone?: string; + hour12?: boolean; +} + function __is(t: T): t is V { return true; } @@ -164,6 +180,12 @@ namespace __Array { return this.join(); } + // the elements' text joined by ",". The locale and its options are taken, as JavaScript takes + // them, but not applied: there is no locale data, so it is the text toString gives. + function toLocaleString(this: T[], locales?: string | string[], options?: ToLocaleStringOptions) { + return this.join(); + } + // A callback takes the element and its index, as JavaScript gives them; one that takes the element only // is given as one taking both (the compiler's fewer-parameters thunk). function every(this: T[], func: (v: T, i: number) => boolean) { @@ -1163,7 +1185,8 @@ class TypedArray { return this.join(); } - public toLocaleString() { + // the locale and its options are taken but not applied, as for an array + public toLocaleString(locales?: string | string[], options?: ToLocaleStringOptions) { return this.join(); } @@ -1562,7 +1585,8 @@ class ConvertingTypedArray { return this.join(); } - public toLocaleString() { + // the locale and its options are taken but not applied, as for an array + public toLocaleString(locales?: string | string[], options?: ToLocaleStringOptions) { return this.join(); } @@ -2824,13 +2848,20 @@ class TextDecoder { // and these are the functions over such values. A rejected promise is a thrown exception. There is // no `then`: a value has no methods. Each is generic, as `isLockFree` is above. namespace Promise { - function resolve(value: T): T { + // with no value: a settled promise of nothing, for `await Promise.resolve()` + function resolve(value?: T): T { return value; } // throws the reason: what awaiting a rejected promise does. The reason keeps its own type, so a - // `catch (e: Error)` catches an Error. - function reject(reason: R): T { + // `catch (e: Error)` catches an Error. With no reason it throws undefined, as JavaScript does + // (in an any: undefined itself can't be thrown). + function reject(reason?: R): T { + if (reason === undefined) { + const nothing: any = undefined; + throw nothing; + } + throw reason; } diff --git a/src/lib.d.ts b/src/lib.d.ts index 08beab3..be9b2b8 100644 --- a/src/lib.d.ts +++ b/src/lib.d.ts @@ -358,7 +358,7 @@ declare namespace __String { function search(this: string, regexp: RegExp): int; - function slice(this: string, indexStart: int, indexEnd?: int): string; + function slice(this: string, indexStart?: int, indexEnd?: int): string; function split(this: string, separator?: string | RegExp, limit?: int): string[]; @@ -441,7 +441,7 @@ declare class String { search(this: string, regexp: RegExp): int; - slice(this: string, indexStart: int, indexEnd?: int): string; + slice(this: string, indexStart?: int, indexEnd?: int): string; split(this: string, separator?: string | RegExp, limit?: int): string[]; diff --git a/src/lib.ts b/src/lib.ts index 7ed33af..9151bb6 100644 --- a/src/lib.ts +++ b/src/lib.ts @@ -1143,7 +1143,7 @@ namespace __String { return regexp.search(this); } - export function slice(this: string, indexStart: int, indexEnd: int = this.length): string { + export function slice(this: string, indexStart: int = 0, indexEnd: int = this.length): string { const len: int = this.length; if (indexStart < 0) { diff --git a/tests/array_toLocaleString.ts b/tests/array_toLocaleString.ts new file mode 100644 index 0000000..da113be --- /dev/null +++ b/tests/array_toLocaleString.ts @@ -0,0 +1,24 @@ +// toLocaleString takes a locale and options, as in JavaScript; with no locale data it gives the +// text toString gives. + +function main() { + const a = [1, 2, 3]; + assert(a.toLocaleString() == "1,2,3", "array toLocaleString()"); + assert(a.toLocaleString("en-US") == "1,2,3", "array toLocaleString(locale)"); + assert(a.toLocaleString("en-US", { style: "currency", currency: "EUR" }) == "1,2,3", "array toLocaleString(locale, options)"); + assert(a.toLocaleString(["en-US", "fr"]) == "1,2,3", "array toLocaleString(locales)"); + + const s = ["x" + 1, "y"]; + assert(s.toLocaleString("fr", { timeZone: "UTC" }) == "x1,y", "string array"); + + const i8 = new Int8Array(3); + i8[1] = 5; + assert(i8.toLocaleString() == "0,5,0", "Int8Array toLocaleString()"); + assert(i8.toLocaleString("en-US", { minimumFractionDigits: 2 }) == "0,5,0", "Int8Array toLocaleString(locale, options)"); + + const c = new Uint8ClampedArray(2); + c[0] = 300; + assert(c.toLocaleString("en-US") == "255,0", "Uint8ClampedArray toLocaleString(locale)"); + + print("ALL DONE"); +} diff --git a/tests/promise_no_args.ts b/tests/promise_no_args.ts new file mode 100644 index 0000000..b7bd5e1 --- /dev/null +++ b/tests/promise_no_args.ts @@ -0,0 +1,32 @@ +// Promise.resolve() and Promise.reject() with no argument: resolve gives a settled promise of +// nothing (void), reject throws undefined, as JavaScript rejects with undefined. + +async function done(): Promise { + await Promise.resolve(); +} + +function main() { + await Promise.resolve(); + done(); + + let caught = false; + try { + Promise.reject(); + } catch (e) { + caught = true; + } + + assert(caught, "reject() throws"); + + let caughtTyped = false; + try { + const n: int = Promise.reject(); + print(n); + } catch (e) { + caughtTyped = true; + } + + assert(caughtTyped, "reject() throws"); + + print("ALL DONE"); +} diff --git a/tests/string_slice_no_args.ts b/tests/string_slice_no_args.ts new file mode 100644 index 0000000..e76893f --- /dev/null +++ b/tests/string_slice_no_args.ts @@ -0,0 +1,18 @@ +// slice() with no arguments copies the whole string: indexStart defaults to 0, as in JavaScript. + +function main() { + const str = "abc" + 1; + + assert(str.slice() == "abc1", "slice()"); + assert(str.slice(undefined, 2) == "ab", "slice(undefined, 2)"); + assert("".slice() == "", "empty slice()"); + + let x = str; + for (let i = 0; i < 3; i++) { + x = x.slice(); + } + + assert(x == "abc1", "slice() in a loop"); + + print("ALL DONE"); +} From 4699b4fc8625a870918c82273be20579f52391fe Mon Sep 17 00:00:00 2001 From: ASDAlexander77 Date: Sat, 21 Nov 2026 10:08:57 +0000 Subject: [PATCH 2/3] String wrapper class: slice() with no arguments too, as declared (check_declarations) Co-Authored-By: Claude Opus 5.5 --- src/lib.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/lib.ts b/src/lib.ts index 9151bb6..268adaa 100644 --- a/src/lib.ts +++ b/src/lib.ts @@ -1539,7 +1539,7 @@ export class String { return this.value.search(regexp); } - public slice(indexStart: int, indexEnd?: int): string { + public slice(indexStart?: int, indexEnd?: int): string { return this.value.slice(indexStart, indexEnd); } From 71939ad0e60dbea1701ac96c89b1170cdcaff232 Mon Sep 17 00:00:00 2001 From: ASDAlexander77 Date: Sat, 21 Nov 2026 10:16:53 +0000 Subject: [PATCH 3/3] Iteration type names: Iterable, IterableIterator, IteratorResult, Generator and the async ones TypeScript code names these types (the compiler-tests sweep: "generic type Iterable/Generator/ AsyncGenerator can't be found"). Here they fit the generators this compiler makes: - IteratorResult is an alias of IterateResult: as an interface a generator's next() did not match it. - Iterable / AsyncIterable are objects with next(), as for...of takes them, not objects with [Symbol.iterator]: a generator has none, and an interface Iterable { next } declared in a program merges with the library's (two compiler tests declare one). - Generator, IterableIterator, AsyncGenerator, AsyncIterableIterator, AsyncIterator: next() of their own; TReturn and TNext are taken, not used. Not yet, in the compiler: an array or a string as an Iterable, a yielded int as a declared number, next(value), return() and throw(); under -mm=own a generator of owned elements returned as an interface declared outside the library. Co-Authored-By: Claude Opus 5.5 --- src/types/lib.types.d.ts | 38 ++++++++++++++++++++++ tests/iteration_types.ts | 70 ++++++++++++++++++++++++++++++++++++++++ 2 files changed, 108 insertions(+) create mode 100644 tests/iteration_types.ts diff --git a/src/types/lib.types.d.ts b/src/types/lib.types.d.ts index d18bfcf..78749ae 100644 --- a/src/types/lib.types.d.ts +++ b/src/types/lib.types.d.ts @@ -10,4 +10,42 @@ interface ClassIterator { next(): IterateResult; } +// TypeScript's iteration types, as far as a generator here can be one. A generator function returns +// an object with next(), and for...of takes any object with next(), so here an Iterable is such an +// object too (not one with [Symbol.iterator]): `function* g(): Generator`, and a generator +// given to an Iterable or Iterator parameter, work. TReturn and TNext are taken but not used: +// next() takes no value, and a returned value is one more value. IteratorResult is an alias, not an +// interface, so a generator's next() matches it. +// Not yet (compiler): an array or a string as an Iterable, next(value), return() and throw(). +type IteratorResult = IterateResult; + +interface Iterable { + next: () => IterateResult; +} + +interface IterableIterator { + next: () => IterateResult; +} + +interface Generator { + next: () => IterateResult; +} + +// Promise is T: an async generator's next() gives the result itself +interface AsyncIterator { + next: () => Promise>; +} + +interface AsyncIterable { + next: () => Promise>; +} + +interface AsyncIterableIterator { + next: () => Promise>; +} + +interface AsyncGenerator { + next: () => Promise>; +} + type time_t = long; \ No newline at end of file diff --git a/tests/iteration_types.ts b/tests/iteration_types.ts new file mode 100644 index 0000000..66c5bfc --- /dev/null +++ b/tests/iteration_types.ts @@ -0,0 +1,70 @@ +// TypeScript's iteration type names: a generator function can be declared to return a Generator, +// IterableIterator or Iterator, an async one an AsyncGenerator, and a generator is given to +// Iterable / AsyncIterable parameters (here an Iterable is an object with next(), as for...of takes). +// Number elements: under -mm=own, a generator returned as an interface of owned elements is not +// followed yet (a compiler limit, as for any interface declared outside the library). + +function* numbers(): Generator { + yield 1.5; + yield 2; +} + +function* counted(n: int): IterableIterator { + // a number: the declared element type does not convert a yielded int yet (compiler) + for (let i: number = 0; i < n; i++) { + yield i; + } +} + +function* typed(): Generator { + yield 4; + yield 5.5; +} + +async function* later(): AsyncGenerator { + yield 0.5; + yield 1; +} + +function sum(values: Iterable) { + let s: number = 0; + for (const v of values) { + s += v; + } + + return s; +} + +function sumOf(it: Iterator) { + let s: number = 0; + for (const v of it) { + s += v; + } + + return s; +} + +async function sumLater(values: AsyncIterable) { + let s: number = 0; + for await (const v of values) { + s += v; + } + + return s; +} + +function main() { + assert(sum(numbers()) == 3.5, "Generator as an Iterable"); + assert(sum(counted(4)) == 6, "IterableIterator as an Iterable"); + assert(sumOf(typed()) == 9.5, "Generator as an Iterator"); + + const it = numbers(); + const first: IteratorResult = it.next(); + assert(first.value == 1.5 && !first.done, "IteratorResult"); + it.next(); + assert(it.next().done, "finished"); + + assert(await sumLater(later()) == 1.5, "AsyncGenerator as an AsyncIterable"); + + print("ALL DONE"); +}