Skip to content

test not working when Record type with default param #167

@usageness

Description

@usageness

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?

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions