type Test<T extends Record<symbol, unknown>> = T;
type Result = Test<{
a: string;
}>;
I made this T extends Record<symbol, unknown>
, Test only accept index type with symbol key,why Test<{a:string}> no false alarms ?
CodePudding user response:
Typescript interfaces are "open", meaning that additional properties can be present on an object outside of its known interface, and this is not a compile time error.
For this reason, a value of type {a: string}
is a valid value for Record<symbol, unknown>
.
You can verify this with the following code that compiles perfectly fine:
const obj: {a: string} = {a: "foo"};
const record: Record<symbol, unknown> = obj;
CodePudding user response:
If you want it to be invalid you can intersect it with another Record whose values are never
:
type Test<T extends Record<symbol, unknown> & Record<string | number, never>> = T;;
type Result = Test<{ // ! error, string/number keys should be type never
a: string;
}>;
Works well enough