Skip to content

Instantly share code, notes, and snippets.

@wycats
Created November 4, 2022 16:10
Show Gist options
  • Save wycats/f8d86917ca53dbe80fcf2b25f59e351f to your computer and use it in GitHub Desktop.
Save wycats/f8d86917ca53dbe80fcf2b25f59e351f to your computer and use it in GitHub Desktop.

October 25, 2022 to November 4, 2022

Generics around missingness

function Name<S extends string | undefined>(name: S): S {
  return name;
}

But this doesn't work:

function Name<S extends string | undefined>(name?: S): S {
  return name;
  //     ~~~~ Type 'T | undefined' is not assignable to type 'T'
}

Name(); // type is string | undefined

Object Literal Overloads

const x = {
  foo(a: string) => string,
  foo(a: number) => string,
  foo(a: string | number) => a {
    return a;
  }
};

Can't auto-infer getters

class X {
  // this can be auto-inferred
  foo() {
    return 1;
  }

  // this can't
  get foo() {
    return 1;
  }
}

import() auto-completes to ImportAssertions

type X = import|
//             ^ if I type `(` here, it completes to `ImportAssertions`

This is because ( is a commit character, and "accept suggestions on commit characters" is on by default.

split() treats the first element of the array as nullable

const [a]: [string] = "".split(" ");
//    ~~~ Type 'string[]' is not assignable to type '[string]'.
//        Target requires 1 element(s) but source may have fewer

Unlisted Property Narrowing is Still Not Quite Right

declare const x: Record<string, string>;

if ("a" in x) {
  const y: string = x["a"];
  //    ~ Type 'string | undefined' is not assignable to type 'string'.
}

I have exactOptionalPropertyTypes and noUncheckedIndexedAccess on. I can see why this is happening, but with exactOptionalPropertyTypes on, I would expect the type of x["a"] to be string after the in check.

override is Great for Jump-to-Definition, But Doesn't Exist for implements

interface Hello {
  hello(): string;
}

class X implements Hello {
  override hello() {
    //         ~~~~~ This member cannot have an 'override' modifier because
    //               its containing class 'X' does not extend another class
    return "hello";
  }
}

What about this?

interface Hello {
  hello(): string;
}

class X implements Hello {
  implements hello() {
    return "hello";
  }
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment