Description
π Search Terms
satisfies upcast
, satisfies changes typechecking behavior
π Version & Regression Information
- This is the behavior in every version I tried, and I reviewed the FAQ for entries about
satisfies
β― Playground Link
π» Code
type Table = "block" | "collection" | "space"
type RecordPointer<T extends Table> = {
[T_ in T]: {
id: string,
table: T_
}
}[T]
function g<T extends Table>(t: T): RecordPointer<Table> {
// Uncommenting `satisfies RecordPointer<Table>` affects whether this program typechecks.
const x = { table: t, id: "foo" } as const // satisfies RecordPointer<Table>
return x
}
π Actual behavior
This program fails to typecheck as written:
Type '{ readonly table: T; readonly id: "foo"; }' is not assignable to type 'RecordPointer<Table>'.
Type '{ readonly table: T; readonly id: "foo"; }' is not assignable to type '{ id: string; table: "space"; }'.
Types of property 'table' are incompatible.
Type 'T' is not assignable to type '"space"'.
Type 'Table' is not assignable to type '"space"'.
Type '"block"' is not assignable to type '"space"'.(2322)
However, if we uncomment the satisfies
expression on the second-to-last line, the type error goes away:
function g<T extends Table>(t: T): RecordPointer<Table> {
- const x = { table: t, id: "foo" } as const
+ const x = { table: t, id: "foo" } as const satisfies RecordPointer<Table>
return x
}
π Expected behavior
My understanding from reading #47920 is that:
satisfies
does not perform safe upcaste satisfies T
should have the inferred typetypeof e
, rather thanT
orT & typeof e
Assuming both of these are correct, I would not expect satisfies
to affect whether this function typechecks.
Additional information about the issue
No response