conditional-type-checks icon indicating copy to clipboard operation
conditional-type-checks copied to clipboard

IsExact fails with string enum item and string

Open MikelArnaiz opened this issue 2 years ago • 1 comments

Hi,

I can't make IsExact work with enums.

const foo = 'Foo';
enum A {
  A1 = 'A1',
  A2 = 'A2',
}

export const t1 = [foo, ['A1']] as const;
export type T1 = typeof t1[number];

export type T2 = typeof foo | readonly [A.A1]

assert<IsExact<T1, T2>>(true);

In the example above

  • T1 is "Foo" | readonly ["A1"]
  • T2 is "Foo" | readonly [A.A1]

I presume the issue is that for the type system "A1" is not exact to A.A1

Edit. Following your another option approach (that doesn't require this library), I can see the origin of the error, which is extremely helpful.

image

MikelArnaiz avatar Apr 08 '22 11:04 MikelArnaiz

A simpler form of this would be:

enum A {
  A1 = 'A1',
  A2 = 'A2',
}
assert<IsExact<"A1", A.A1>>(true); // fails

This is failing for the same reason that the following fails:

enum A {
  A1 = 'A1',
  A2 = 'A2',
}
  
// Type '"A1"' is not assignable to type 'A.A1'.
const value: A.A1 = "A1";

So I think this is failing by design.

dsherret avatar Jun 18 '22 19:06 dsherret