tsdjs / tsd

Check TypeScript type definitions
MIT License
2.39k stars 68 forks source link

Problem with expectAssignable and expectNotAssignable #190

Open XadillaX opened 1 year ago

XadillaX commented 1 year ago

I have an interface like this:

import { NumbersToN } from 'ts-number-range';
type Byte = NumbersToN<256>;
interface RGBColor {
  r: Byte;
  g: Byte;
  b: Byte;
}

Then I test:

expectAssignable<RGBColor>({ r: 0, g: 0, b: 0 });  // ✅ it passes
expectAssignable<RGBColor>({ r: 0, g: 0, b: 256 });  // ✅ it failed
expectNotAssignable<RGBColor>({ r: 0, g: 0, b: 0 });  // ❌ it passes
expectNotAssignable<RGBColor>({ r: 0, g: 0, b: 256 });  // ✅ it passes

It seems expectNotAssignable<RGBColor>({ r: 0, g: 0, b: 0 }) not work correctly.

mrazauskas commented 1 year ago

Did you try:

expectAssignable<RGBColor>({r: 0, g: 0, b: 0} as const);
expectAssignable<RGBColor>({r: 0, g: 0, b: 256} as const);

expectNotAssignable<RGBColor>({r: 0, g: 0, b: 0} as const);
expectNotAssignable<RGBColor>({r: 0, g: 0, b: 256} as const);

Because:

Screenshot 2023-03-30 at 10 38 56

But:

Screenshot 2023-03-30 at 10 38 42
XadillaX commented 1 year ago

Did you try:

expectAssignable<RGBColor>({r: 0, g: 0, b: 0} as const);
expectAssignable<RGBColor>({r: 0, g: 0, b: 256} as const);

expectNotAssignable<RGBColor>({r: 0, g: 0, b: 0} as const);
expectNotAssignable<RGBColor>({r: 0, g: 0, b: 256} as const);

Because:

Screenshot 2023-03-30 at 10 38 56

But:

Screenshot 2023-03-30 at 10 38 42

It works after I adding as const.