Skip to content

cannot enforce type shape when assigned to a conjunction #28642

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

Closed
sledorze opened this issue Nov 21, 2018 · 5 comments
Closed

cannot enforce type shape when assigned to a conjunction #28642

sledorze opened this issue Nov 21, 2018 · 5 comments
Labels
In Discussion Not yet reached consensus Suggestion An idea for TypeScript

Comments

@sledorze
Copy link

TypeScript Version: 3.1.6, 3.2.0-rc, 3.3.0-dev.20181121

Search Terms:
conjunction assignable wrong type

Code

declare const a: { a: string; b: number }
declare const b: { a: string } & { b: number }
declare function f(x: { [k: string]: string }): void
f(a) // error
f(b) // NO error

Expected behavior:
Errors on both f(a) and f(b)

Actual behavior:
Errors only on f(a)

Playground Link: http://www.typescriptlang.org/play/#src=declare%20const%20a%3A%20%7B%20a%3A%20string%3B%20b%3A%20number%20%7D%0D%0Adeclare%20const%20b%3A%20%7B%20a%3A%20string%20%7D%20%26%20%7B%20b%3A%20number%20%7D%0D%0Adeclare%20function%20f(x%3A%20%7B%20%5Bk%3A%20string%5D%3A%20string%20%7D)%3A%20void%0D%0Af(a)%20%2F%2F%20error%0D%0Af(b)%20%2F%2F%20NO%20error

Related Issues: gcanti/io-ts#234 (comment)
gcanti/fp-ts-routing#37

@sledorze sledorze changed the title conjunction assignable wrong type cannot enforce type shape when assigned to a conjunction Nov 21, 2018
@sledorze
Copy link
Author

I'm guessing this type relaxation is required in certain scenario (with named members), however, with an indexed type on string (covering both 'a' and 'b'), I would rather expect it to fail.

@jack-williams
Copy link
Collaborator

jack-williams commented Nov 21, 2018

From the spec: S is a subtype of T if S is an intersection type and at least one constituent type of S is a subtype of T. (same for assignability).

  • T & U is assignable to X if T is assignable to X.
  • { a: string } is assignable to { [k: string]: string }

String indexers are unsound like this when properties are 'forgotten'. Another example:

  • { a : string; b: number } is assignable to { a: string }
  • { a: string } is assignable to { [k: string]: string }
const a: { a: string; b: number } = { a: 'foo', b: 42 };
const b: { a: string } = a
const c: { [k: string]: string } = c;
c.b = "not a number"
const shouldBeNumber: number = a.b;

@sledorze
Copy link
Author

@jack-williams indeed.
The amount of consequences of the unsoundness is frightening.

@MartinJohns
Copy link
Contributor

@jack-williams Quoting the spec is not advised, as the spec was last updated around 3 years ago.

@jakebailey
Copy link
Member

This was fixed by #35143, whose description contains effectively this issue's repro

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
In Discussion Not yet reached consensus Suggestion An idea for TypeScript
Projects
None yet
Development

No branches or pull requests

5 participants