Skip to content

Literal Tuples/'as const' - allow lib creator to mark type parameters as 'as const' to allow for better TS DSLsΒ #59380

Closed

Description

πŸ” Search Terms

  • tuple as const
  • literal tuple as const

βœ… Viability Checklist

⭐ Suggestion

The simplest thing that comes to mind, is to be able to use 'as const' in type definition, and have arguments of function using this type have 'as const' modifier active without specifying it on the side of function call

const test = <Tuple extends Array<any> as const>(data: Unique<Tuple>) => {}

test([
	{ type: 'Error-Code', value: 100 },
	{ type: 'Error-Code', value: 200 },
// duplicate causes 'never' to be argument type
        { type: 'Error-Code', value: 200 },
])

πŸ“ƒ Motivating Example

Simplified example:
Lets say you are creating DSL/Lib that often uses Tuples with object Literals and you want to make more extensive type checks.
The current solution is to use 'as const' after the argument.

This sadly has some issues:

  • not really enforceable even though you really want check every single time when the function is used
  • repeated boilerplate code that uglifies the code quite a bit
  • really confusing for users that have a little knowledge of TS, and you just wont them to fill in data

It would be great if this responsibility could be optionally redirected to DSL/lib creators, so that Type params that are always meant to be used with Tuples of literal values like object literals, would be marked as such by them.

Following simple example illustrates the problem - I want to check for duplicate entries in sample DSL and must always use 'as const' for proper type checks.

// Checks tuple for duplicates - this will fail to work correctly without 'as const'
type Unique<Tuple extends any[]> = ...

it would be great if I could specify 'as const' for Tuple type param
const test = <Tuple extends Array<any>>(data: Unique<Tuple>) => {}

test([
	{ type: 'Error-Code', value: 100 },
	{ type: 'Error-Code', value: 200 },
// here 'as const' must be specified to work, which is inconvenient
] as const)

πŸ’» Use Cases

  1. What do you want to use this for?
    DSLs and libs that use tuples as function arguments often
  2. What shortcomings exist with current approaches?
    Forcing users of libs/DSLs to write 'as const' every time they want to have more powerful typechecks on tuples with literals/object literals
  3. What workarounds are you using in the meantime?
    Using 'as const' is the current workaround

Activity

  1. jcalz commented on Jul 21, 2024

    @jcalz
    Contributor
  2. jkavalec commented on Jul 21, 2024

    @jkavalec
    Author

    Thank you, this relatively new feature completely flew under my radar. And no Google/SO related search did show any relevant results

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

    FixedA PR has been merged for this issue

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions