microsoft / microsoft/TypeScript

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

Aperta
#58,656 0 commenti 2 reazioni 0 assegnatari Vedi su GitHub

Nessuno ha ancora preso questa issue.

Awaiting More Feedback Suggestion
Lingua principale
Go
Stelle
111k
Fork
14.4k
Merge medio
1g 19h
PR unite (30g)
117

Descrizione

🔍 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

Guida per i contributori

Apri la guida per i contributori

Come iniziare

  1. Leggi tutta la issue e poi la guida ai contributi del progetto.
  2. Commenta sulla issue per dire che te ne occupi tu — evita che due persone facciano lo stesso lavoro.
  3. Fai un fork del repository e lavora su un branch.
  4. Apri una pull request che faccia riferimento al numero della issue.

Direzione di ricerca

Inizia riproducendo l'esempio del parametro di tipo const e rivedendo la TypeScript compiler API e l'approccio interno esistente usato da ts-api-utils PR #519. Il lavoro è completato quando un'API ufficialmente supportata può determinare se un'espressione argomento si trova in un contesto const, incluso il caso d'uso readonly-type, senza fare affidamento sugli internals di TypeScript.

Scritto dal modello di indicizzazione a partire dal testo della issue.

Valutazione

Stack tecnologico
typescript
Ambito
compilers
Tipo di issue
Funzionalità
Difficoltà
5/5
Tempo stimato
Più di una settimana
Stato di attività
Ferma
Chiarezza
Abbastanza chiara
Idoneità per principianti
30/100

Ricevi le nuove issue nella tua casella

Un breve riepilogo di issue GitHub adatte ai principianti.