Skip to content

cannot enforce type shape when assigned to a conjunction #28642

Closed

Description

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

Activity

  1. changed the title [-]conjunction assignable wrong type[/-] [+]cannot enforce type shape when assigned to a conjunction[/+] on Nov 21, 2018
  2. sledorze commented on Nov 21, 2018

    @sledorze
    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.

  3. jack-williams commented on Nov 21, 2018

    @jack-williams
    Collaborator

    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;
  4. sledorze commented on Nov 21, 2018

    @sledorze
    Author

    Jack Williams (@jack-williams) indeed.
    The amount of consequences of the unsoundness is frightening.

  5. MartinJohns commented on Nov 21, 2018

    @MartinJohns
    Contributor

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

  6. jakebailey commented on Jul 11, 2023

    @jakebailey
    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

Metadata

Metadata

Assignees

No one assigned

    Labels

    In DiscussionNot yet reached consensusSuggestionAn idea for TypeScript

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions