Skip to content

Instantly share code, notes, and snippets.

@ralph
Last active August 23, 2024 08:33
Show Gist options
  • Select an option

  • Save ralph/c33ffd92b6b67751dbb06efb88282031 to your computer and use it in GitHub Desktop.

Select an option

Save ralph/c33ffd92b6b67751dbb06efb88282031 to your computer and use it in GitHub Desktop.

Revisions

  1. ralph revised this gist Jun 21, 2023. No changes.
  2. ralph revised this gist Jun 21, 2023. No changes.
  3. ralph created this gist Jun 21, 2023.
    12 changes: 12 additions & 0 deletions test1-output.txt
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,12 @@
    src/test.ts:10:39 - error TS18049: 'item' is possibly 'null' or 'undefined'.

    10 strings.forEach((item) => console.log(item.id, item.label))
    ~~~~

    src/test.ts:10:48 - error TS18049: 'item' is possibly 'null' or 'undefined'.

    10 strings.forEach((item) => console.log(item.id, item.label))
    ~~~~


    Found 2 errors in the same file, starting at: src/test.ts
    10 changes: 10 additions & 0 deletions test1.ts
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,10 @@
    const stringsAndNull = [
    { id: 1, label: 'foo' },
    { id: 2, label: 'bar' },
    null,
    { id: 3, label: 'test' },
    undefined,
    { id: 3, label: 'lol' },
    ]
    const strings = stringsAndNull.filter((item) => item != null)
    strings.forEach((item) => console.log(item.id, item.label))
    7 changes: 7 additions & 0 deletions test2-output.txt
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,7 @@
    src/test.ts:10:17 - error TS2345: Argument of type '(item: { id: number; label: string;}) => void' is not assignable to parameter of type '(value: { id: number; label: string; } | null | undefined, index: number, array: ({ id: number; label: string; } | null | undefined)[]) => void'.
    Types of parameters 'item' and 'value' are incompatible.
    Type '{ id: number; label: string; } | null | undefined' is not assignable to type '{ id: number; label: string; }'.
    Type 'undefined' is not assignable to type '{ id: number; label: string; }'.

    10 strings.forEach((item: { id: number, label: string}) => console.log(item.id, item.label))
    ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    10 changes: 10 additions & 0 deletions test2.ts
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,10 @@
    const stringsAndNull = [
    { id: 1, label: 'foo' },
    { id: 2, label: 'bar' },
    null,
    { id: 3, label: 'test' },
    undefined,
    { id: 3, label: 'lol' },
    ]
    const strings = stringsAndNull.filter((item) => item != null)
    strings.forEach((item: { id: number, label: string }) => console.log(item.id, item.label))