Skip to content

[API] Ability to check if a parameter is being passed to const type parameterĀ #58656

@RebeccaStevens

Description

@RebeccaStevens

šŸ” Search Terms

const type parameter

āœ… Viability Checklist

⭐ Suggestion

Hi from ts-api-utils.

We'd like to be able to check if an expression, passed as an argument of a call expression, is in a "const context". Checking for as const is fairly trivial as we can just search up the syntax tree, but checking for const type parameters seems impossible without using TypeScript internals.

With code examples:

We can currently check if foo is in a "const context" here fairly trivially:

const test1 = { foo: 1 } as const;

But can't do the same for foo in this example:

declare const fn: <const A>(param: A) => unknown;
const test2 = fn({ foo: 2 });

Proposal

Ideally, we would like an officially supported way to make such a check.

We currently have a PR in progress that works using ts internals, but we'd prefer not to rely on ts internals.

šŸ“ƒ Motivating Example

N/A

šŸ’» Use Cases

  1. What do you want to use this for?
    determining whether a type is readonly
    Determine immutability for const type parametersĀ eslint-functional/eslint-plugin-functional#827

  2. What shortcomings exist with current approaches?
    There doesn't seem to be anyway official way to do this

  3. What workarounds are you using in the meantime?
    Use ts internals

Metadata

Metadata

Assignees

No one assigned

    Labels

    Awaiting More FeedbackThis means we'd like to hear from more people who would be helped by this featureSuggestionAn idea for TypeScript

    Type

    No type
    No fields configured for issues without a type.

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions

      ⚔