Skip to content

Commit 8b21698

Browse files
authored
fix(csv): remove undefined from possible value type of parse result (denoland#5617)
As we discussed in denoland#5605 (comment), it seems like we never get `undefined` as a parse result of fields. If there is a mismatch in the number of fields across rows, the parse just throws an error. To better reflect this in typing, this commit removes `undefined` from the record value type.
1 parent b0f2088 commit 8b21698

File tree

5 files changed

+15
-16
lines changed

5 files changed

+15
-16
lines changed

csv/_io.ts

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -250,14 +250,13 @@ export type ParseResult<ParseOptions, T> =
250250
T extends ParseOptions & { columns: readonly (infer C extends string)[] }
251251
? RecordWithColumn<C>[]
252252
// If `skipFirstRow` option is specified, the return type is Record type.
253-
: T extends ParseOptions & { skipFirstRow: true }
254-
? Record<string, string | undefined>[]
253+
: T extends ParseOptions & { skipFirstRow: true } ? Record<string, string>[]
255254
// If `columns` and `skipFirstRow` option is _not_ specified, the return type is string[][].
256255
: T extends
257256
ParseOptions & { columns?: undefined; skipFirstRow?: false | undefined }
258257
? string[][]
259258
// else, the return type is Record type or string[][].
260-
: Record<string, string | undefined>[] | string[][];
259+
: Record<string, string>[] | string[][];
261260

262261
/**
263262
* Record type with column type.
@@ -269,5 +268,5 @@ export type ParseResult<ParseOptions, T> =
269268
* ```
270269
*/
271270
export type RecordWithColumn<C extends string> = string extends C
272-
? Record<string, string | undefined>
271+
? Record<string, string>
273272
: Record<C, string>;

csv/parse.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -388,7 +388,7 @@ export function parse(input: string): string[][];
388388
* const result = parse(string, { skipFirstRow: true });
389389
*
390390
* assertEquals(result, [{ a: "d", b: "e", c: "f" }]);
391-
* assertType<IsExact<typeof result, Record<string, string | undefined>[]>>(true);
391+
* assertType<IsExact<typeof result, Record<string, string>[]>>(true);
392392
* ```
393393
*
394394
* @example Specify columns with `columns` option

csv/parse_stream.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -148,7 +148,7 @@ export type RowType<T> = T extends undefined ? string[]
148148
* { name: "Alice", age: "34" },
149149
* { name: "Bob", age: "24" },
150150
* ]);
151-
* assertType<IsExact<typeof result, Record<string, string | undefined>[]>>(true);
151+
* assertType<IsExact<typeof result, Record<string, string>[]>>(true);
152152
* ```
153153
*
154154
* @example Specify columns with `columns` option

csv/parse_stream_test.ts

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -491,13 +491,13 @@ Deno.test({
491491
// `skipFirstRow` may be `true` or `false`.
492492
// `columns` may be `undefined` or `string[]`.
493493
// If you don't know exactly what the value of the option is,
494-
// the return type is ReadableStream<string[] | Record<string, string | undefined>>
494+
// the return type is ReadableStream<string[] | Record<string, string>>
495495
const options: CsvParseStreamOptions = {};
496496
const { readable } = new CsvParseStream(options);
497497
type _ = AssertTrue<
498498
IsExact<
499499
typeof readable,
500-
ReadableStream<string[] | Record<string, string | undefined>>
500+
ReadableStream<string[] | Record<string, string>>
501501
>
502502
>;
503503
}
@@ -520,7 +520,7 @@ Deno.test({
520520
type _ = AssertTrue<
521521
IsExact<
522522
typeof readable,
523-
ReadableStream<Record<string, string | undefined>>
523+
ReadableStream<Record<string, string>>
524524
>
525525
>;
526526
}
@@ -541,7 +541,7 @@ Deno.test({
541541
type _ = AssertTrue<
542542
IsExact<
543543
typeof readable,
544-
ReadableStream<Record<string, string | undefined>>
544+
ReadableStream<Record<string, string>>
545545
>
546546
>;
547547
}
@@ -562,7 +562,7 @@ Deno.test({
562562
type _ = AssertTrue<
563563
IsExact<
564564
typeof readable,
565-
ReadableStream<Record<string, string | undefined>>
565+
ReadableStream<Record<string, string>>
566566
>
567567
>;
568568
}

csv/parse_test.ts

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -943,13 +943,13 @@ Deno.test({
943943
// `skipFirstRow` may be `true` or `false`.
944944
// `columns` may be `undefined` or `string[]`.
945945
// If you don't know exactly what the value of the option is,
946-
// the return type is string[][] | Record<string, string|undefined>[]
946+
// the return type is string[][] | Record<string, string>[]
947947
const options: ParseOptions = {};
948948
const parsed = parse("a\nb", options);
949949
type _ = AssertTrue<
950950
IsExact<
951951
typeof parsed,
952-
string[][] | Record<string, string | undefined>[]
952+
string[][] | Record<string, string>[]
953953
>
954954
>;
955955
}
@@ -970,7 +970,7 @@ Deno.test({
970970
{
971971
const parsed = parse("a\nb", { skipFirstRow: true });
972972
type _ = AssertTrue<
973-
IsExact<typeof parsed, Record<string, string | undefined>[]>
973+
IsExact<typeof parsed, Record<string, string>[]>
974974
>;
975975
}
976976

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

@@ -1000,7 +1000,7 @@ Deno.test({
10001000
{
10011001
const parsed = parse("a\nb", { skipFirstRow: true, columns: undefined });
10021002
type _ = AssertTrue<
1003-
IsExact<typeof parsed, Record<string, string | undefined>[]>
1003+
IsExact<typeof parsed, Record<string, string>[]>
10041004
>;
10051005
}
10061006
{

0 commit comments

Comments
 (0)