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

Add support for inferring tuple types generated from using the spread operator on const assertions #37357

Closed
4 of 5 tasks
btoo opened this issue Mar 11, 2020 · 4 comments
Closed
4 of 5 tasks
Labels
In Discussion Not yet reached consensus Suggestion An idea for TypeScript

Comments

@btoo
Copy link

btoo commented Mar 11, 2020

Search Terms

spread tuple const assertion

Suggestion

I've noticed that when using the spread operator with as const variables, TypeScript loses tuples' index-to-type information, contrary to object literals, which get their structure (key-to-type) copied over:

const a = { b: 1, c: 2 } as const
/**
 * correctly inferred as
 * const e: {
 *  readonly f: 3;
 *  readonly b: 1;
 *  readonly c: 2;
 * }
 */
const e = { ...a, f: 3 } as const

const g = [1, 2] as const
/**
 * inferred as
 * const h: readonly (1 | 2 | 3)[]
 * but would prefer
 * const h: readonly [1, 2, 3]
 */
const h = [...g, 3] as const

(reproduction link)

Is this due to the same underlying issue present in #34589?

Use Cases

This would allow us to "extend" tuple types

Examples

const workweekDays = ['Monday', 'Tuesday', 'Wednesday', 'Thursday', 'Friday'] as const
const weekendDays = ['Saturday', 'Sunday'] as const

/**
 * inferred as
 * const days: readonly ("Monday" | "Tuesday" | "Wednesday" | "Thursday" | "Friday" | "Saturday" | "Sunday")[]
 * but would prefer
 * const days: readonly ["Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday", "Sunday"]
 */
const days = [...workweekDays, ...weekendDays] as const

(reproduction link)

Checklist

My suggestion meets these guidelines:

  • This wouldn't be a breaking change in existing TypeScript/JavaScript code
  • This wouldn't change the runtime behavior of existing JavaScript code
  • This could be implemented without emitting different JS based on the types of the expressions
  • This isn't a runtime feature (e.g. library functionality, non-ECMAScript syntax with JavaScript output, etc.)
  • This feature would agree with the rest of TypeScript's Design Goals.
@RyanCavanaugh RyanCavanaugh added In Discussion Not yet reached consensus Suggestion An idea for TypeScript labels Mar 12, 2020
@RyanCavanaugh
Copy link
Member

@ahejlsberg this feels more like a bug to me; the intended behavior here seems unambiguous. Thoughts?

@ahejlsberg
Copy link
Member

Looks like a duplicate of #33783. This is a design limitation that we could consider lifting.

@somebody1234
Copy link

if this is a duplicate, should this be closed?

@btoo
Copy link
Author

btoo commented Aug 10, 2021

this is fixed with #36861! closing now

@btoo btoo closed this as completed Aug 10, 2021
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

4 participants