microsoft / microsoft/TypeScript
[API] Ability to check if a parameter is being passed to const type parameter
Nadie ha tomado este issue todavía.
- Lenguaje dominante
- Go
- Estrellas
- 111k
- Forks
- 14.4k
- Merge medio
- 1 d 19 h
- PR fusionados (30 d)
- 117
Descripción
🔍 Search Terms
const type parameter
✅ Viability Checklist
- This wouldn't be a breaking change in existing TypeScript/JavaScript code
- This wouldn't change the runtime behavior of existing JavaScript code
- This could be implemented without emitting different JS based on the types of the expressions
- This isn't a runtime feature (e.g. library functionality, non-ECMAScript syntax with JavaScript output, new syntax sugar for JS, etc.)
- This isn't a request to add a new utility type: https://github.com/microsoft/TypeScript/wiki/No-New-Utility-Types
- This feature would agree with the rest of our Design Goals: https://github.com/Microsoft/TypeScript/wiki/TypeScript-Design-Goals
⭐ 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
-
What do you want to use this for?
determining whether a type is readonly
https://github.com/eslint-functional/eslint-plugin-functional/issues/827 -
What shortcomings exist with current approaches?
There doesn't seem to be anyway official way to do this -
What workarounds are you using in the meantime?
Use ts internals
Guía de contribución
Primeros pasos
- Lee el issue completo y luego la guía de contribución del proyecto.
- Comenta en el issue que vas a ocuparte — evita que dos personas hagan lo mismo.
- Haz un fork del repositorio y trabaja en una rama.
- Abre un pull request que haga referencia al número del issue.
Línea de trabajo
Comienza reproduciendo el ejemplo de parámetro de tipo const y revisando la TypeScript compiler API y el enfoque interno existente utilizado por ts-api-utils PR #519. Se considera terminado cuando una API oficialmente compatible pueda determinar si una expresión de argumento está en un contexto const, incluido el caso de uso readonly-type, sin depender de los elementos internos de TypeScript.
Escrito por el modelo de indexación a partir del texto del issue.
Evaluación
- Stack tecnológico
- typescript
- Área
- compilers
- Tipo de issue
- Nueva funcionalidad
- Dificultad
- 5/5
- Tiempo estimado
- Más de una semana
- Estado de actividad
- Estancado
- Claridad
- Bastante claro
- Aptitud para principiantes
- 30/100