Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(csv): remove undefined from possible value type of parse result #5617

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 3 additions & 4 deletions csv/_io.ts
Original file line number Diff line number Diff line change
Expand Up @@ -250,14 +250,13 @@ export type ParseResult<ParseOptions, T> =
T extends ParseOptions & { columns: readonly (infer C extends string)[] }
? RecordWithColumn<C>[]
// If `skipFirstRow` option is specified, the return type is Record type.
: T extends ParseOptions & { skipFirstRow: true }
? Record<string, string | undefined>[]
: T extends ParseOptions & { skipFirstRow: true } ? Record<string, string>[]
// If `columns` and `skipFirstRow` option is _not_ specified, the return type is string[][].
: T extends
ParseOptions & { columns?: undefined; skipFirstRow?: false | undefined }
? string[][]
// else, the return type is Record type or string[][].
: Record<string, string | undefined>[] | string[][];
: Record<string, string>[] | string[][];

/**
* Record type with column type.
Expand All @@ -269,5 +268,5 @@ export type ParseResult<ParseOptions, T> =
* ```
*/
export type RecordWithColumn<C extends string> = string extends C
? Record<string, string | undefined>
? Record<string, string>
: Record<C, string>;
2 changes: 1 addition & 1 deletion csv/parse.ts
Original file line number Diff line number Diff line change
Expand Up @@ -388,7 +388,7 @@ export function parse(input: string): string[][];
* const result = parse(string, { skipFirstRow: true });
*
* assertEquals(result, [{ a: "d", b: "e", c: "f" }]);
* assertType<IsExact<typeof result, Record<string, string | undefined>[]>>(true);
* assertType<IsExact<typeof result, Record<string, string>[]>>(true);
* ```
*
* @example Specify columns with `columns` option
Expand Down
2 changes: 1 addition & 1 deletion csv/parse_stream.ts
Original file line number Diff line number Diff line change
Expand Up @@ -148,7 +148,7 @@ export type RowType<T> = T extends undefined ? string[]
* { name: "Alice", age: "34" },
* { name: "Bob", age: "24" },
* ]);
* assertType<IsExact<typeof result, Record<string, string | undefined>[]>>(true);
* assertType<IsExact<typeof result, Record<string, string>[]>>(true);
* ```
*
* @example Specify columns with `columns` option
Expand Down
10 changes: 5 additions & 5 deletions csv/parse_stream_test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -491,13 +491,13 @@ Deno.test({
// `skipFirstRow` may be `true` or `false`.
// `columns` may be `undefined` or `string[]`.
// If you don't know exactly what the value of the option is,
// the return type is ReadableStream<string[] | Record<string, string | undefined>>
// the return type is ReadableStream<string[] | Record<string, string>>
const options: CsvParseStreamOptions = {};
const { readable } = new CsvParseStream(options);
type _ = AssertTrue<
IsExact<
typeof readable,
ReadableStream<string[] | Record<string, string | undefined>>
ReadableStream<string[] | Record<string, string>>
>
>;
}
Expand All @@ -520,7 +520,7 @@ Deno.test({
type _ = AssertTrue<
IsExact<
typeof readable,
ReadableStream<Record<string, string | undefined>>
ReadableStream<Record<string, string>>
>
>;
}
Expand All @@ -541,7 +541,7 @@ Deno.test({
type _ = AssertTrue<
IsExact<
typeof readable,
ReadableStream<Record<string, string | undefined>>
ReadableStream<Record<string, string>>
>
>;
}
Expand All @@ -562,7 +562,7 @@ Deno.test({
type _ = AssertTrue<
IsExact<
typeof readable,
ReadableStream<Record<string, string | undefined>>
ReadableStream<Record<string, string>>
>
>;
}
Expand Down
10 changes: 5 additions & 5 deletions csv/parse_test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -943,13 +943,13 @@ Deno.test({
// `skipFirstRow` may be `true` or `false`.
// `columns` may be `undefined` or `string[]`.
// If you don't know exactly what the value of the option is,
// the return type is string[][] | Record<string, string|undefined>[]
// the return type is string[][] | Record<string, string>[]
const options: ParseOptions = {};
const parsed = parse("a\nb", options);
type _ = AssertTrue<
IsExact<
typeof parsed,
string[][] | Record<string, string | undefined>[]
string[][] | Record<string, string>[]
>
>;
}
Expand All @@ -970,7 +970,7 @@ Deno.test({
{
const parsed = parse("a\nb", { skipFirstRow: true });
type _ = AssertTrue<
IsExact<typeof parsed, Record<string, string | undefined>[]>
IsExact<typeof parsed, Record<string, string>[]>
>;
}

Expand All @@ -988,7 +988,7 @@ Deno.test({
{
const parsed = parse("a\nb", { columns: ["aaa"] as string[] });
type _ = AssertTrue<
IsExact<typeof parsed, Record<string, string | undefined>[]>
IsExact<typeof parsed, Record<string, string>[]>
>;
}

Expand All @@ -1000,7 +1000,7 @@ Deno.test({
{
const parsed = parse("a\nb", { skipFirstRow: true, columns: undefined });
type _ = AssertTrue<
IsExact<typeof parsed, Record<string, string | undefined>[]>
IsExact<typeof parsed, Record<string, string>[]>
>;
}
{
Expand Down