Skip to content

satisfies changes typechecking behaviorΒ #60698

Closed as duplicate of#52394
Closed as duplicate of#52394
@sliminality

Description

@sliminality

πŸ”Ž 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

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 upcast
  • e satisfies T should have the inferred type typeof e, rather than T or T & 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

Metadata

Metadata

Assignees

No one assigned

    Labels

    DuplicateAn existing issue was already created

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions