microsoft / microsoft/TypeScript
Suggestion: Reference expression type during cast
Nadie ha tomado este issue todavía.
- Lenguaje dominante
- Go
- Estrellas
- 111k
- Forks
- 14.3k
- Merge medio
- 2 d 4 h
- PR fusionados (30 d)
- 132
Descripción
Search Terms
cast reference expression type
Suggestion
I would like to be able to reference original type of the expression during cast.
For example:
const o = {
x: { a: 1 } as typeof & { b?: number }
}
// type of o.x is now { a: number; b?: number }
I am not attached to typeof syntax here, can be anything that makes sense.
Use Cases
- Defining optional properties without actually initializing them.
Current approach would require me to do something like:
const o = {
x: { a: 1 } as { a: number; b?: number }
}
which is unweildy and repeats all the properties.
It is possible to do through a function, but it has an overhead and is complicated due to partial generics:
function extend<T>(T data) {
return <TExtra>() => data as T & TExtra;
}
const o = {
x: extend({ a: 1 })<{ b?: number }>()
}
- Adding indexers to object literals:
function map1(key: string) {
return { a: 1, b: 2 }[key] || 3; // does not work due to https://github.com/microsoft/TypeScript/issues/14951
}
function map2(key: string) {
return ({ a: 1, b: 2 } as { [key: string]: typeof[keyof typeof]|undefined })[key] || 3;
}
Examples
See use cases.
Checklist
My suggestion meets these guidelines:
- 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, etc.)
- This feature would agree with the rest of TypeScript's Design Goals.
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
En el issue no se nombra ningún archivo fuente, prueba ni punto de entrada. Empieza revisando la sintaxis de cast propuesta y ambos casos de uso, y determina después el diseño necesario del sistema de tipos y la sintaxis; se consideraría terminado con un enfoque aceptado y el alcance de implementación correspondiente.
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
- Necesita aclaración
- Aptitud para principiantes
- 30/100