-
Notifications
You must be signed in to change notification settings - Fork 12.8k
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
Comments
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. |
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).
String indexers are unsound like this when properties are 'forgotten'. Another example:
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; |
@jack-williams indeed. |
@jack-williams Quoting the spec is not advised, as the spec was last updated around 3 years ago. |
This was fixed by #35143, whose description contains effectively this issue's repro |
TypeScript Version: 3.1.6, 3.2.0-rc, 3.3.0-dev.20181121
Search Terms:
conjunction assignable wrong type
Code
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
The text was updated successfully, but these errors were encountered: