microsoft / microsoft/TypeScript

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

Offen
#58,656 0 Kommentare 2 Reaktionen 0 zugewiesene Personen Auf GitHub ansehen

Dieses Issue hat noch niemand übernommen.

Awaiting More Feedback Suggestion
Vorherrschende Sprache
Go
Sterne
111k
Forks
14.4k
Ø Merge
1 T. 19 Std.
Gemergte PRs (30 T.)
117

Beschreibung

🔍 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

Beitragsleitfaden

Beitragsleitfaden öffnen

Erste Schritte

  1. Lies das ganze Issue und danach den Beitragsleitfaden des Projekts.
  2. Schreib ins Issue, dass du es übernimmst — das erspart doppelte Arbeit.
  3. Forke das Repository und arbeite in einem Branch.
  4. Öffne einen Pull Request, der die Issue-Nummer nennt.

Rechercherichtung

Beginne damit, das Beispiel für den const-Typparameter nachzustellen und die TypeScript compiler API sowie den bestehenden internen Ansatz aus ts-api-utils PR #519 zu überprüfen. Als erledigt gilt die Aufgabe, wenn eine offiziell unterstützte API bestimmen kann, ob sich ein Argumentausdruck in einem const-Kontext befindet, einschließlich des readonly-type-Anwendungsfalls, ohne sich auf TypeScript-Interna zu stützen.

Vom Indexierungsmodell aus dem Issue-Text verfasst.

Bewertung

Tech-Stack
typescript
Bereich
compilers
Issue-Typ
Feature
Schwierigkeit
5/5
Geschätzter Aufwand
Über eine Woche
Aktivitätsstatus
Veraltet
Klarheit
Größtenteils klar
Anfängerfreundlichkeit
30/100

Neue Issues direkt in Ihr Postfach

Eine kurze Übersicht über anfängerfreundliche GitHub-Issues.