microsoft / microsoft/TypeScript

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

Open
#58,656 0 comments 2 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

Awaiting More Feedback Suggestion
Dominant language
Go
Stars
111k
Forks
14.4k
Avg merge
1d 19h
Merged PRs (30d)
117

Description

🔍 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
    https://github.com/eslint-functional/eslint-plugin-functional/issues/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

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start by reproducing the const type parameter example and reviewing the TypeScript compiler API and the existing internal approach used by ts-api-utils PR #519. Done means an officially supported API can determine whether an argument expression is in a const context, including the readonly-type use case, without relying on TypeScript internals.

Written by the indexing model from the issue text.

Assessment

Tech stack
typescript
Domain
compilers
Issue type
Feature
Difficulty
5/5
Estimated time
Over a week
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
30/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.