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

[micro-dash] Feature request: make isEmpty() a type guard #59

Open
eric-simonton-sama opened this issue Nov 1, 2021 · 3 comments
Open

Comments

@eric-simonton-sama
Copy link
Collaborator

No description provided.

@ersimont
Copy link
Member

ersimont commented Nov 3, 2021

While many cases could be covered nicely with a simple implementation, it would be complicated to make correct in all cases. Take this for example:

type EmptyObject = Record<any, never>;
declare function isEmpty(
  value: any,
): value is Nil | Primitive | readonly [] | EmptyObject;

declare const numOrArray: number | number[];
if (isEmpty(numOrArray)) {
  // should narrow to `1 | []`
  // but is `number`
}

@ersimont
Copy link
Member

ersimont commented Nov 3, 2021

The test I started to write before deciding other things have priority:

  it('has fancy typing', () => {
    interface O {
      a: Date;
    }
    const o = {} as O;

    const oOrN = o as O | null;
    if (isEmpty(oOrN)) {
      expectTypeOf(oOrN).toEqualTypeOf<null>();
    } else {
      expectTypeOf(oOrN).toEqualTypeOf<O>();
    }

    const oOrE = o as O | {};
    if (isEmpty(oOrE)) {
      expectTypeOf(oOrE).toEqualTypeOf<{}>();
    } else {
      expectTypeOf(oOrE).toEqualTypeOf<O>();
    }

    const numOrArray = 1 as number | number[];
    if (isEmpty(numOrArray)) {
      expectTypeOf(numOrArray).toEqualTypeOf<number | []>();
    } else {
      expectTypeOf(numOrArray).toEqualTypeOf<number[]>();
    }
  });

@ersimont
Copy link
Member

Some more tests. These are easy to get passing, but that last one above is not!

      const obj = {} as {} | Date;
      if (isEmpty(obj)) {
        expectTypeOf(obj).toEqualTypeOf<{}>();
      } else {
        expectTypeOf(obj).toEqualTypeOf<Date>();
      }

      const eobj = {} as EmptyObject | Date;
      if (isEmpty(eobj)) {
        expectTypeOf(eobj).toEqualTypeOf<EmptyObject>();
      } else {
        expectTypeOf(eobj).toEqualTypeOf<Date>();
      }

      const robj = {} as Readonly<EmptyObject> | Date;
      if (isEmpty(robj)) {
        expectTypeOf(robj).toEqualTypeOf<Readonly<EmptyObject>>();
      } else {
        expectTypeOf(robj).toEqualTypeOf<Date>();
      }

      const ary = [] as [] | Date;
      if (isEmpty(ary)) {
        expectTypeOf(ary).toEqualTypeOf<[]>();
      } else {
        expectTypeOf(ary).toEqualTypeOf<Date>();
      }

      const rary = [] as readonly [] | Date;
      if (isEmpty(rary)) {
        expectTypeOf(rary).toEqualTypeOf<readonly []>();
      } else {
        expectTypeOf(rary).toEqualTypeOf<Date>();
      }

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

No branches or pull requests

2 participants