Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

satisfies changes typechecking behavior #60698

Closed
sliminality opened this issue Dec 6, 2024 · 2 comments Β· May be fixed by #60710
Closed

satisfies changes typechecking behavior #60698

sliminality opened this issue Dec 6, 2024 · 2 comments Β· May be fixed by #60710
Labels
Duplicate An existing issue was already created

Comments

@sliminality
Copy link

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

@MartinJohns
Copy link
Contributor

This sounds like a duplicate of #52394.

@RyanCavanaugh RyanCavanaugh added the Duplicate An existing issue was already created label Dec 6, 2024
@sliminality
Copy link
Author

You're totally right, I missed that. Thanks!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Duplicate An existing issue was already created
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants