Skip to content

Array spreads should be contextually typed #42037

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

Open
mournie opened this issue Dec 18, 2020 · 4 comments
Open

Array spreads should be contextually typed #42037

mournie opened this issue Dec 18, 2020 · 4 comments
Labels
In Discussion Not yet reached consensus Suggestion An idea for TypeScript

Comments

@mournie
Copy link

mournie commented Dec 18, 2020

TypeScript Version: 3.8

Search Terms:

  • array literal type
  • array spread type inference
  • implicit array literal type
  • array destructuring type inference [sic]

Response to Original Issue Post

We have "special" logic for unifying object literals in these cases; the spread syntax interferes with that.

If you post a more representative sample where it's not just spreading for the sake of spreading, we may be able to advise more.

Originally posted by @RyanCavanaugh in #30761 (comment)

Real Use Case

The common pattern of literal array assignment with conditional inclusion of elements is one use case.

Code

type t = 'a'

// The common pattern
const a:t[] = [
  ...(true?['a']:[]),
]

// Minimal repro of phenomenon
const a:t[] = [
  ...['a']
]

The error messages given above are both Type 'string[]' is not assignable to type '"a"[]'.

Of course if the type of the nested array is asserted or otherwise known, the spread doesn't get in the way of that - it only seems to interrupt the automatic inference of the literal. We get as specific as string, but not as specific as the actual value.

@DanielRosenwasser
Copy link
Member

DanielRosenwasser commented Dec 18, 2020

I think that issue is not quite related.

This seems pretty reasonable, though in the future please follow the template.

const a: ("foo" | "bar")[] = [
    ...(Math.random() < 0.5 ? ["foo"] : []),
];

Playground Link

@DanielRosenwasser DanielRosenwasser added In Discussion Not yet reached consensus Suggestion An idea for TypeScript labels Dec 18, 2020
@DanielRosenwasser DanielRosenwasser changed the title No inference of array literal nested spread types Array spreads should be contextually typed Dec 18, 2020
@mournie
Copy link
Author

mournie commented Dec 19, 2020

This seems pretty reasonable, though in the future please follow the template.

Yeah sorry - I used the "reference in new issue" button and it pre-filled the above quote instead of the standard template, so I just went with that.

@mournie
Copy link
Author

mournie commented Dec 19, 2020

I think that issue is not quite related.

I guess object spread types are inferred in a way analogous to the way in which the array example isn't, so here's another example.

type a = {one: 1}
type b = 'b'[]

const a:a = {...{one:1}} // work
const b:b = [...['b']] // not work

@rentalhost
Copy link

Any updates on this case?

type Case = [string?]

const caseTrue: Case = [...(true ? ["abc" ] : [])];
const caseFalse: Case = [...(false ? ["abc"] : [])];

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

3 participants