From e8ea3b426eca87e2da5545783b99863025da7667 Mon Sep 17 00:00:00 2001 From: Chris Krycho Date: Fri, 1 May 2026 12:29:32 -0600 Subject: [PATCH 1/3] =?UTF-8?q?=F0=9F=A4=96=20Merge=20PR=20#74939=20Update?= =?UTF-8?q?=20chance=20types=20to=20be=20module-first,=20global-second=20b?= =?UTF-8?q?y=20@chriskrycho?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- types/chance/chance-tests.ts | 8 +- types/chance/index.d.ts | 691 ++++++++++++++++++++--------------- 2 files changed, 394 insertions(+), 305 deletions(-) diff --git a/types/chance/chance-tests.ts b/types/chance/chance-tests.ts index 5cac8822287ffc..ce4f9c6e8e07b0 100644 --- a/types/chance/chance-tests.ts +++ b/types/chance/chance-tests.ts @@ -1,9 +1,15 @@ -import { Chance } from "chance"; +import { Chance, type Currency, type Options, type Seed } from "chance"; // Instantiation const chance = new Chance(); const createYourOwn = new Chance(Math.random); +const moduleSeed: Seed = "module-seed"; +const moduleChance: Chance = new Chance(moduleSeed); +const moduleOptions: Options = { likelihood: 30, min: 1, max: 10 }; +const moduleOptionValue = moduleOptions.likelihood; +const moduleCurrency: Currency = moduleChance.currency(); + // Basic usage let bool: boolean = chance.bool(); bool = chance.bool({ likelihood: 30 }); diff --git a/types/chance/index.d.ts b/types/chance/index.d.ts index d21d10b4bb48b6..c359e5bc2f414f 100644 --- a/types/chance/index.d.ts +++ b/types/chance/index.d.ts @@ -1,339 +1,422 @@ -declare namespace Chance { - type Seed = number | string; +interface ChanceStatic { + (): ChanceInstance; + (...seed: Seed[]): ChanceInstance; + (generator: () => any): ChanceInstance; - interface Seeded { - seed: Seed; - } + new(): ChanceInstance; + new(...seed: Seed[]): ChanceInstance; + new(generator: () => any): ChanceInstance; - interface ChanceStatic { - (): Chance; - (...seed: Seed[]): Chance; - (generator: () => any): Chance; + Chance: ChanceStatic; +} - new(): Chance; - new(...seed: Seed[]): Chance; - new(generator: () => any): Chance; - } +declare var Chance: ChanceStatic; - type FalsyType = false | null | undefined | 0 | typeof NaN | ""; - interface FalsyOptions { - pool: FalsyType[]; - } +type Seed = number | string; - interface Chance extends Seeded { - // Basics - bool(opts?: { likelihood: number }): boolean; - character(opts?: Partial): string; - /** https://chancejs.com/basics/falsy.html */ - falsy(ops?: FalsyOptions): FalsyType; - floating(opts?: Options): number; - integer(opts?: Partial): number; - letter(opts?: Options): string; - natural(opts?: Options): number; - string(opts?: Partial): string; - template(template: string): string; - - // Text - paragraph(opts?: Options): string; - sentence(opts?: Partial): string; - syllable(opts?: Options): string; - word(opts?: Partial): string; - - // Person - age(opts?: Options): number; - gender(): string; - birthday(): Date; - birthday(opts?: Options): Date | string; - cf(opts?: Options): string; - cpf(opts?: { formatted: boolean }): string; - first(opts?: Partial): string; - last(opts?: LastNameOptions): string; - name(opts?: Partial): string; - name_prefix(opts?: Partial): string; - name_suffix(opts?: SuffixOptions): string; - prefix(opts?: Partial): string; - ssn(opts?: Options): string; - suffix(opts?: SuffixOptions): string; - - // Mobile - animal(opts?: Options): string; - - // Mobile - android_id(): string; - apple_token(): string; - bb_pin(): string; - wp7_anid(): string; - wp8_anid2(): string; - - // Web - avatar(opts?: Options): string; - color(opts?: Options): string; - company(): string; - domain(opts?: Options): string; - email(opts?: Partial): string; - fbid(): string; - google_analytics(): string; - hashtag(): string; - ip(): string; - ipv6(): string; - klout(): string; - profession(opts?: Options): string; - tld(): string; - twitter(): string; - url(opts?: Partial): string; - mac_address(opts?: Partial): string; - - // Location - address(opts?: Options): string; - altitude(opts?: Options): number; - areacode(): string; - city(): string; - coordinates(opts?: Options): string; - country(opts?: Options): string; - depth(opts?: Options): number; - geohash(opts?: Options): string; - latitude(opts?: Options): number; - locale(opts?: { region: true }): string; - locales(opts?: { region: true }): string[]; - longitude(opts?: Options): number; - phone(opts?: Options): string; - postcode(): string; - postal(): string; - province(opts?: Options): string; - state(opts?: Options): string; - street(opts?: Options): string; - zip(opts?: Options): string; - - // Time - ampm(): string; - date(): Date; - date(opts: DateOptions): Date | string; - hammertime(): number; - hour(opts?: Options): number; - millisecond(): number; - minute(): number; - month(): string; - month(opts: Options): Month; - second(): number; - timestamp(): number; - timezone(): Timezone; - weekday(opts: Options): "Monday" | "Tuesday" | "Wednesday" | "Thursday" | "Friday" | "Saturday" | "Sunday"; - year(opts?: Options): string; - - // Finance - cc(opts?: Options): string; - cc_type(): string; - cc_type(opts: Options): string | CreditCardType; - currency(): Currency; - currency_pair(): [Currency, Currency]; - dollar(opts?: Options): string; - euro(opts?: Options): string; - exp(): string; - exp(opts: Options): string | CreditCardExpiration; - exp_month(opts?: Options): string; - exp_year(opts?: Options): string; - - // Helpers - capitalize(str: string): string; - mixin(desc: MixinDescriptor): any; - pad(num: number, width: number, padChar?: string): string; - /** - * @deprecated Use pickone - */ - pick(arr: readonly T[]): T; - pickone(arr: readonly T[]): T; - /** - * @deprecated Use pickset - */ - pick(arr: readonly T[], count: number): T[]; - pickset(arr: readonly T[], count?: number): T[]; - set: Setter; - shuffle(arr: readonly T[]): T[]; - - // Miscellaneous - coin(): string; - d4(): number; - d6(): number; - d8(): number; - d10(): number; - d12(): number; - d20(): number; - d30(): number; - d100(): number; - guid(options?: { version: 4 | 5 }): string; - hash(opts?: Options): string; - n(generator: () => T, count: number): T[]; - n(generator: (options: O) => T, count: number, options: O): T[]; - normal(opts?: Options): number; - radio(opts?: Options): string; - rpg(dice: string): number[]; - rpg(dice: string, opts?: Options): number[] | number; - tv(opts?: Options): string; - unique(generator: () => T, count: number): T[]; - unique>(generator: (options: O) => T, count: number, options: O): T[]; - weighted(values: T[], weights: number[]): T; - - // "Hidden" - cc_types(): CreditCardType[]; - mersenne_twister(seed?: Seed): any; // API return type not defined in docs - months(): Month[]; - name_prefixes(): Name[]; - provinces(): Name[]; - states(): Name[]; - street_suffix(): Name; - street_suffixes(): Name[]; - } +interface Seeded { + seed: Seed; +} - // A more rigorous approach might be to produce - // the correct options interfaces for each method - interface Options { - [id: string]: any; - } +type FalsyType = false | null | undefined | 0 | typeof NaN | ""; +interface FalsyOptions { + pool: FalsyType[]; +} - interface WordOptions { - length: number; - syllables: number; - capitalize: boolean; - } +interface ChanceInstance extends Seeded { + // Basics + bool(opts?: { likelihood: number }): boolean; + character(opts?: Partial): string; + /** https://chancejs.com/basics/falsy.html */ + falsy(ops?: FalsyOptions): FalsyType; + floating(opts?: Options): number; + integer(opts?: Partial): number; + letter(opts?: Options): string; + natural(opts?: Options): number; + string(opts?: Partial): string; + template(template: string): string; + + // Text + paragraph(opts?: Options): string; + sentence(opts?: Partial): string; + syllable(opts?: Options): string; + word(opts?: Partial): string; + + // Person + age(opts?: Options): number; + gender(): string; + birthday(): Date; + birthday(opts?: Options): Date | string; + cf(opts?: Options): string; + cpf(opts?: { formatted: boolean }): string; + first(opts?: Partial): string; + last(opts?: LastNameOptions): string; + name(opts?: Partial): string; + name_prefix(opts?: Partial): string; + name_suffix(opts?: SuffixOptions): string; + prefix(opts?: Partial): string; + ssn(opts?: Options): string; + suffix(opts?: SuffixOptions): string; + + // Mobile + animal(opts?: Options): string; + + // Mobile + android_id(): string; + apple_token(): string; + bb_pin(): string; + wp7_anid(): string; + wp8_anid2(): string; + + // Web + avatar(opts?: Options): string; + color(opts?: Options): string; + company(): string; + domain(opts?: Options): string; + email(opts?: Partial): string; + fbid(): string; + google_analytics(): string; + hashtag(): string; + ip(): string; + ipv6(): string; + klout(): string; + profession(opts?: Options): string; + tld(): string; + twitter(): string; + url(opts?: Partial): string; + mac_address(opts?: Partial): string; + + // Location + address(opts?: Options): string; + altitude(opts?: Options): number; + areacode(): string; + city(): string; + coordinates(opts?: Options): string; + country(opts?: Options): string; + depth(opts?: Options): number; + geohash(opts?: Options): string; + latitude(opts?: Options): number; + locale(opts?: { region: true }): string; + locales(opts?: { region: true }): string[]; + longitude(opts?: Options): number; + phone(opts?: Options): string; + postcode(): string; + postal(): string; + province(opts?: Options): string; + state(opts?: Options): string; + street(opts?: Options): string; + zip(opts?: Options): string; + + // Time + ampm(): string; + date(): Date; + date(opts: DateOptions): Date | string; + hammertime(): number; + hour(opts?: Options): number; + millisecond(): number; + minute(): number; + month(): string; + month(opts: Options): Month; + second(): number; + timestamp(): number; + timezone(): Timezone; + weekday(opts: Options): "Monday" | "Tuesday" | "Wednesday" | "Thursday" | "Friday" | "Saturday" | "Sunday"; + year(opts?: Options): string; + + // Finance + cc(opts?: Options): string; + cc_type(): string; + cc_type(opts: Options): string | CreditCardType; + currency(): Currency; + currency_pair(): [Currency, Currency]; + dollar(opts?: Options): string; + euro(opts?: Options): string; + exp(): string; + exp(opts: Options): string | CreditCardExpiration; + exp_month(opts?: Options): string; + exp_year(opts?: Options): string; + + // Helpers + capitalize(str: string): string; + mixin(desc: MixinDescriptor): any; + pad(num: number, width: number, padChar?: string): string; + pickone(arr: readonly T[]): T; + /** + * @deprecated Use pickset + */ + pick(arr: readonly T[]): T; + /** + * @deprecated Use pickset + */ + pick(arr: readonly T[], count: number): T[]; + pickset(arr: readonly T[], count?: number): T[]; + set: Setter; + shuffle(arr: readonly T[]): T[]; + + // Miscellaneous + coin(): string; + d4(): number; + d6(): number; + d8(): number; + d10(): number; + d12(): number; + d20(): number; + d30(): number; + d100(): number; + guid(options?: { version: 4 | 5 }): string; + hash(opts?: Options): string; + n(generator: () => T, count: number): T[]; + n(generator: (options: O) => T, count: number, options: O): T[]; + normal(opts?: Options): number; + radio(opts?: Options): string; + rpg(dice: string): number[]; + rpg(dice: string, opts?: Options): number[] | number; + tv(opts?: Options): string; + unique(generator: () => T, count: number): T[]; + unique>(generator: (options: O) => T, count: number, options: O): T[]; + weighted(values: T[], weights: number[]): T; + + // "Hidden" + cc_types(): CreditCardType[]; + mersenne_twister(seed?: Seed): any; // API return type not defined in docs + months(): Month[]; + name_prefixes(): Name[]; + provinces(): Name[]; + states(): Name[]; + street_suffix(): Name; + street_suffixes(): Name[]; +} - interface CharacterOptions { - casing: "upper" | "lower"; - pool: string; - alpha: boolean; - numeric: boolean; - symbols: boolean; - } +// A more rigorous approach might be to produce +// the correct options interfaces for each method +interface Options { + [id: string]: any; +} - type StringOptions = CharacterOptions & { length: number }; +interface WordOptions { + length: number; + syllables: number; + capitalize: boolean; +} - interface UrlOptions { - protocol: string; - domain: string; - domain_prefix: string; - path: string; - extensions: string[]; - } +interface CharacterOptions { + casing: "upper" | "lower"; + pool: string; + alpha: boolean; + numeric: boolean; + symbols: boolean; +} - interface MacOptions { - separator: string; - networkVersion: boolean; - } +type StringOptions = CharacterOptions & { length: number }; - interface IntegerOptions { - min: number; - max: number; - } +interface UrlOptions { + protocol: string; + domain: string; + domain_prefix: string; + path: string; + extensions: string[]; +} - type FirstNameNationalities = "en" | "it"; - type LastNameNationalities = FirstNameNationalities | "nl" | "uk" | "de" | "jp" | "es" | "fr" | "*"; +interface MacOptions { + separator: string; + networkVersion: boolean; +} - interface FullNameOptions { - middle: boolean; - middle_initial: boolean; - prefix: boolean; - suffix: boolean; - } +interface IntegerOptions { + min: number; + max: number; +} - interface FirstNameOptions { - gender: "male" | "female"; - nationality: FirstNameNationalities; - } +type FirstNameNationalities = "en" | "it"; +type LastNameNationalities = FirstNameNationalities | "nl" | "uk" | "de" | "jp" | "es" | "fr" | "*"; - interface LastNameOptions { - nationality: LastNameNationalities; - } +interface FullNameOptions { + middle: boolean; + middle_initial: boolean; + prefix: boolean; + suffix: boolean; +} - interface SuffixOptions { - full: boolean; - } +interface FirstNameOptions { + gender: "male" | "female"; + nationality: FirstNameNationalities; +} - type PrefixOptions = { gender: "male" | "female" | "all" } & SuffixOptions; +interface LastNameOptions { + nationality: LastNameNationalities; +} - type NameOptions = FullNameOptions & FirstNameOptions & LastNameOptions & PrefixOptions; +interface SuffixOptions { + full: boolean; +} - interface EmailOptions { - length: number; - domain: string; - } +type PrefixOptions = { gender: "male" | "female" | "all" } & SuffixOptions; - interface SentenceOptions { - words: number; - punctuation: "." | "?" | ";" | "!" | ":" | boolean; - } +type NameOptions = FullNameOptions & FirstNameOptions & LastNameOptions & PrefixOptions; - interface DateOptions { - string?: boolean | undefined; - american?: boolean | undefined; - year?: number | undefined; - month?: number | undefined; - day?: number | undefined; - min?: Date | undefined; - max?: Date | undefined; - } +interface EmailOptions { + length: number; + domain: string; +} + +interface SentenceOptions { + words: number; + punctuation: "." | "?" | ";" | "!" | ":" | boolean; +} - type UniqueOptions = { comparator?: ((array: T[], value: T) => boolean) | undefined } & Options; +interface DateOptions { + string?: boolean | undefined; + american?: boolean | undefined; + year?: number | undefined; + month?: number | undefined; + day?: number | undefined; + min?: Date | undefined; + max?: Date | undefined; +} - interface Month { - name: string; - short_name: string; - numeric: string; - } +type UniqueOptions = { comparator?: ((array: T[], value: T) => boolean) | undefined } & Options; - interface CreditCardType { - name: string; - short_name: string; - prefix: string; - length: number; - } +interface Month { + name: string; + short_name: string; + numeric: string; +} - interface Currency { - code: string; - name: string; - } +interface CreditCardType { + name: string; + short_name: string; + prefix: string; + length: number; +} - interface CreditCardExpiration { - month: string; - year: string; - } +interface Currency { + code: string; + name: string; +} - interface MixinDescriptor { - [id: string]: (...args: any[]) => any; - } +interface CreditCardExpiration { + month: string; + year: string; +} - interface Setter { - (key: "firstNames", values: string[]): any; - (key: "lastNames", values: string[]): any; - (key: "provinces", values: string[]): any; - (key: "us_states_and_dc", values: string[]): any; - (key: "territories", values: string[]): any; - (key: "armed_forces", values: string[]): any; - (key: "street_suffixes", values: string[]): any; - (key: "months", values: string[]): any; - (key: "cc_types", values: string[]): any; - (key: "currency_types", values: string[]): any; - (key: string, values: T[]): any; - } +interface MixinDescriptor { + [id: string]: (...args: any[]) => any; +} - interface Name { - name: string; - abbreviation: string; - } +interface Setter { + (key: "firstNames", values: string[]): any; + (key: "lastNames", values: string[]): any; + (key: "provinces", values: string[]): any; + (key: "us_states_and_dc", values: string[]): any; + (key: "territories", values: string[]): any; + (key: "armed_forces", values: string[]): any; + (key: "street_suffixes", values: string[]): any; + (key: "months", values: string[]): any; + (key: "cc_types", values: string[]): any; + (key: "currency_types", values: string[]): any; + (key: string, values: unknown[]): any; +} - interface Timezone { - name: string; - abbr: string; - offset: number; - isdst: boolean; - text: string; - utc: string[]; - } +interface Name { + name: string; + abbreviation: string; } -declare module "chance" { - interface ExportedChance extends Chance.ChanceStatic { - Chance: ExportedChance; - } - var Chance: ExportedChance; +interface Timezone { + name: string; + abbr: string; + offset: number; + isdst: boolean; + text: string; + utc: string[]; +} +declare namespace Chance { + export { + ChanceInstance as Chance, + Seed, + Seeded, + FalsyType, + FalsyOptions, + Options, + WordOptions, + CharacterOptions, + StringOptions, + UrlOptions, + MacOptions, + IntegerOptions, + FirstNameNationalities, + LastNameNationalities, + FullNameOptions, + FirstNameOptions, + LastNameOptions, + SuffixOptions, + PrefixOptions, + NameOptions, + EmailOptions, + SentenceOptions, + DateOptions, + UniqueOptions, + Month, + CreditCardType, + Currency, + CreditCardExpiration, + MixinDescriptor, + Setter, + Name, + Timezone, + }; +} + +export = Chance; + +import ChanceModule = require("./index"); + +declare global { + namespace Chance { + type Seed = ChanceModule.Seed; + + type Seeded = ChanceModule.Seeded; + interface Chance extends ChanceModule.Chance { + seed: ChanceModule.Seed; + } + + type FalsyType = ChanceModule.FalsyType; + type FalsyOptions = ChanceModule.FalsyOptions; + + type Options = ChanceModule.Options; + type WordOptions = ChanceModule.WordOptions; + type CharacterOptions = ChanceModule.CharacterOptions; + type StringOptions = ChanceModule.StringOptions; + type UrlOptions = ChanceModule.UrlOptions; + type MacOptions = ChanceModule.MacOptions; - export = Chance; + type IntegerOptions = ChanceModule.IntegerOptions; + + type FirstNameNationalities = ChanceModule.FirstNameNationalities; + type LastNameNationalities = ChanceModule.LastNameNationalities; + + type FullNameOptions = ChanceModule.FullNameOptions; + type FirstNameOptions = ChanceModule.FirstNameOptions; + type LastNameOptions = ChanceModule.LastNameOptions; + type SuffixOptions = ChanceModule.SuffixOptions; + type PrefixOptions = ChanceModule.PrefixOptions; + type NameOptions = ChanceModule.NameOptions; + + type EmailOptions = ChanceModule.EmailOptions; + type SentenceOptions = ChanceModule.SentenceOptions; + type DateOptions = ChanceModule.DateOptions; + + type UniqueOptions = ChanceModule.UniqueOptions; + + type Month = ChanceModule.Month; + type CreditCardType = ChanceModule.CreditCardType; + type Currency = ChanceModule.Currency; + type CreditCardExpiration = ChanceModule.CreditCardExpiration; + + type MixinDescriptor = ChanceModule.MixinDescriptor; + type Setter = ChanceModule.Setter; + + type Name = ChanceModule.Name; + type Timezone = ChanceModule.Timezone; + } } From 15c04eece8369b3a2dff33aa5a5e595a2db6e217 Mon Sep 17 00:00:00 2001 From: TypeScript Bot Date: Fri, 1 May 2026 18:30:58 +0000 Subject: [PATCH 2/3] =?UTF-8?q?=F0=9F=A4=96=20dprint=20fmt?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- types/chance/index.d.ts | 46 ++++++++++++++++++++--------------------- 1 file changed, 23 insertions(+), 23 deletions(-) diff --git a/types/chance/index.d.ts b/types/chance/index.d.ts index c359e5bc2f414f..c4d61d60867a6b 100644 --- a/types/chance/index.d.ts +++ b/types/chance/index.d.ts @@ -333,37 +333,37 @@ interface Timezone { declare namespace Chance { export { ChanceInstance as Chance, - Seed, - Seeded, - FalsyType, - FalsyOptions, - Options, - WordOptions, CharacterOptions, - StringOptions, - UrlOptions, - MacOptions, - IntegerOptions, + CreditCardExpiration, + CreditCardType, + Currency, + DateOptions, + EmailOptions, + FalsyOptions, + FalsyType, FirstNameNationalities, - LastNameNationalities, - FullNameOptions, FirstNameOptions, + FullNameOptions, + IntegerOptions, + LastNameNationalities, LastNameOptions, - SuffixOptions, - PrefixOptions, + MacOptions, + MixinDescriptor, + Month, + Name, NameOptions, - EmailOptions, + Options, + PrefixOptions, + Seed, + Seeded, SentenceOptions, - DateOptions, - UniqueOptions, - Month, - CreditCardType, - Currency, - CreditCardExpiration, - MixinDescriptor, Setter, - Name, + StringOptions, + SuffixOptions, Timezone, + UniqueOptions, + UrlOptions, + WordOptions, }; } From f414cff77d6656deb7a25b137999d54e3fcd7d3f Mon Sep 17 00:00:00 2001 From: liuxingbaoyu <30521560+liuxingbaoyu@users.noreply.github.com> Date: Sat, 2 May 2026 05:49:50 +0800 Subject: [PATCH 3/3] =?UTF-8?q?=F0=9F=A4=96=20Merge=20PR=20#74920=20[gensy?= =?UTF-8?q?nc]=20Improve=20compatibility=20with=20`strictFunctionTypes`=20?= =?UTF-8?q?by=20@liuxingbaoyu?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- types/gensync/gensync-tests.ts | 19 ++++++++++++------- types/gensync/index.d.ts | 12 ++++++------ 2 files changed, 18 insertions(+), 13 deletions(-) diff --git a/types/gensync/gensync-tests.ts b/types/gensync/gensync-tests.ts index 162aa22e7812d0..f56f82463511cc 100644 --- a/types/gensync/gensync-tests.ts +++ b/types/gensync/gensync-tests.ts @@ -9,7 +9,7 @@ declare function readFileCallback( declare function readFileSync(path: string, encoding: "utf8" | "ascii"): string; declare function readFileAsync(path: string, encoding: "utf8" | "ascii"): Promise; -// $ExpectType Gensync<[path: string, encoding: "utf8" | "ascii"], string, unknown> +// $ExpectType Gensync<[path: string, encoding: "utf8" | "ascii"], string, any> const readFileFromSync = gensync({ name: "readFile", arity: 2, @@ -22,10 +22,10 @@ readFileFromSync.sync; // $ExpectType (path: string, encoding: "utf8" | "ascii") => Promise readFileFromSync.async; -// $ExpectType (path: string, encoding: "utf8" | "ascii", callback: (err: unknown, result: string) => void) => void +// $ExpectType (path: string, encoding: "utf8" | "ascii", callback: (err: any, result: string) => void) => void readFileFromSync.errback; -// $ExpectType Gensync<[path: string, encoding: "utf8" | "ascii"], string, unknown> +// $ExpectType Gensync<[path: string, encoding: "utf8" | "ascii"], string, any> const readFileFromAsync = gensync({ name: "readFile", sync: readFileSync, @@ -38,7 +38,7 @@ readFileFromAsync.sync; // $ExpectType (path: string, encoding: "utf8" | "ascii") => Promise readFileFromAsync.async; -// $ExpectType (path: string, encoding: "utf8" | "ascii", callback: (err: unknown, result: string) => void) => void +// $ExpectType (path: string, encoding: "utf8" | "ascii", callback: (err: any, result: string) => void) => void readFileFromAsync.errback; // $ExpectType Gensync<[path: string, encoding: "utf8" | "ascii"], string, Error> @@ -57,7 +57,7 @@ readFileFromErrback.async; // $ExpectType (path: string, encoding: "utf8" | "ascii", callback: (err: Error, result: string) => void) => void readFileFromErrback.errback; -// $ExpectType Gensync<[], void, unknown> +// $ExpectType Gensync<[], void, any> const noop = gensync(function*() {}); // $ExpectType () => void @@ -66,7 +66,7 @@ noop.sync; // $ExpectType () => Promise noop.async; -// $ExpectType (callback: (err: unknown) => void) => void +// $ExpectType (callback: (err: any) => void) => void noop.errback; gensync({ @@ -99,7 +99,7 @@ async function readContentsAsync() { } readContents.errback("foo", (err, result) => { - // $ExpectType unknown + // $ExpectType any err; // $ExpectType string result; @@ -191,3 +191,8 @@ gensync({ callback(new Error(), "some result"); }, }); + +gensync(function*() {}).errback((err: Error) => { + // $ExpectType Error + err; +}); diff --git a/types/gensync/index.d.ts b/types/gensync/index.d.ts index e8c9aae3208b04..346bbc8465d6e9 100644 --- a/types/gensync/index.d.ts +++ b/types/gensync/index.d.ts @@ -7,7 +7,7 @@ * - `.errback(...args, (err, result) => {})` - Calls the callback with the computed value, or error. * @param generatorFnOrOptions A generator function, or options for an existing sync/async function */ -declare function gensync( +declare function gensync( generatorFnOrOptions: ((...args: A) => Generator) | gensync.Options, ): gensync.Gensync; @@ -16,7 +16,7 @@ declare namespace gensync { * A generator produced by `gensync`, which can only "await" (with `yield*`) other * generators produced by `gensync`. */ - type Handler = Generator; + type Handler = Generator; /** * Given a `gensync` generator, produces the "awaited" type of that generator @@ -28,7 +28,7 @@ declare namespace gensync { * A callback function such that if the result is void, there is no result parameter. */ // eslint-disable-next-line @typescript-eslint/no-invalid-void-type - type Callback = [R] extends [void] ? (err: E) => void : (err: E, result: R) => void; + type Callback = [R] extends [void] ? (err: E) => void : (err: E, result: R) => void; /** * A function that can be "awaited" (with `yield*`) in another `gensync` generator, @@ -38,7 +38,7 @@ declare namespace gensync { * - `.async(...args)` - Returns a promise for the computed value. * - `.errback(...args, (err, result) => {})` - Calls the callback wit */ - interface Gensync { + interface Gensync { (...args: A): Handler; sync(...args: A): R; async(...args: A): Promise; @@ -86,7 +86,7 @@ declare namespace gensync { async: (...args: A) => Promise; } - interface ErrbackOptions extends Omit, "errback"> { + interface ErrbackOptions extends Omit, "errback"> { /** * A function that will be called when `.async()` or `.errback()` is called on * the `gensync()` result, or when the result is passed to `yield*` in another @@ -101,7 +101,7 @@ declare namespace gensync { errback: (...args: [...A, Callback]) => void; } - type Options = + type Options = | SyncOptions | AsyncOptions | ErrbackOptions;