tsdjs / tsd

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

test not working when Record type with default param #167

Open usageness opened 1 year ago

usageness commented 1 year ago

While testing custom type, I met some bug like this:

it("ObjectType type test", () => {
type ObjectType<T, K extends string | number | symbol = string> = Record<
  K,
  T
>;
  // given & when
  const result = { a: "1", b: "2", c: "3", d: "4", e: "5" };

  // then
  expectType<ObjectType<string>>(result); // test passed
  expectType<ObjectType<string, symbol>>(result); // test passed
  expectType<ObjectType<string, number>>(result); // test passed
  expectNotType<ObjectType<string, number>>(result); // test passed
}

I think, may be there's something problem in checking types with default param. So I check another test case.

normal case

type TestType<T extends string | number> = Record<T, string>;
const test = { a: 1 };

expectType<TestType<string>>(test);  // ide warn, test failed
expectNotType<TestType<string>>(test);

problem case

type TestType<T extends string | number = string> = Record<T, string>;
const test = { a: 1 };

expectType<TestType<string>>(test);  // ide warn, but test passed
expectNotType<TestType<string>>(test);  // test passed
type TestType<K, T extends string | number = string> = Record<T, K>;
const test = { a: 1 };

expectType<TestType<string, number>>(test);  // no warning, test passed
expectNotType<TestType<string, number>>(test);  // test passed, problem occurred!

Can anyone please explain this result?

LAH1203 commented 1 year ago

Me too!

skarab42 commented 1 year ago

Duplicate of #142 but fixed in the new API proposal #168