Skip to content

fix(csv-parse): type the delimiter_auto score callback arguments - #499

Merged
wdavidw merged 2 commits into
adaltas:masterfrom
dylanpulver:fix/csv-parse-delimiter-auto-score-type
Sep 23, 2026
Merged

wdavidw merged 2 commits into
adaltas:masterfrom
dylanpulver:fix/csv-parse-delimiter-auto-score-type

Conversation

@dylanpulver

Copy link
Copy Markdown
Contributor

OptionDelimiterAuto.score is declared as () => number, so a custom scoring function cannot be given the two arguments the parser passes it.

parse(input, {
  delimiter_auto: {
    score: (info, options) =>
      (info.total - info.std) * (options.preferred[info.char_code] || 1),
  },
});

Actual, from npx tsc --noEmit:

error TS2769: No overload matches this call.
    Type '(info: any, options: any) => number' is not assignable to type '() => number'.
      Target signature provides too few arguments. Expected 2 or more, but got 0.

Expected: it compiles. utils/delimiter_discover.js:46 calls options.score(info, options), and the default at api/normalize_options.js:237 has exactly that shape. The snippet above is a copy of that default, so writing a custom score in TypeScript needs a cast.

preferred and size went into the interface in the same commit as score (4f69946) and both match the runtime. score is the member that does not.

This types the callback with a ScoringFunction alias sitting next to CastingFunction, plus an InfoDelimiterAuto interface for the five properties delimiter_discover sets before it calls score.

The test added to test/option.delimiter_auto.ts fails tsc --noEmit, which npm test runs ahead of mocha, when the type change is reverted. It also covers the runtime path: a custom score that ranks : above ; flips the discovered delimiter on a:b;c\nd:e;f, and no test passed a custom score before.

readonly total: number;
}

export type ScoringFunction = (

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I propose DelimiterAutoArgs, DelimiterAutoInfo, and DelimiterAutoOptions

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Or ScoringFunction, ScoringFunctionInfo, and ScoringFunctionOptions.

dylanpulver and others added 2 commits September 23, 2026 11:27
Renames InfoDelimiterAuto to ScoringFunctionInfo and OptionDelimiterAuto
to ScoringFunctionOptions, per review, so the three types read as one
family with ScoringFunction.
@wdavidw
wdavidw force-pushed the fix/csv-parse-delimiter-auto-score-type branch from 2fb3b4a to 090ce31 Compare September 23, 2026 09:35
@wdavidw
wdavidw merged commit 4aa04da into adaltas:master Sep 23, 2026
@wdavidw

wdavidw commented Sep 23, 2026

Copy link
Copy Markdown
Member

Thank you very much!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants