microsoft / microsoft/TypeScript
Flattening types in generated .d.ts files for libraries
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
"delcaration readable" "declaration pretty"
✅ 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
Allow library authors to add the inline modifier on a type declaration which makes the compiler flatten the type in generated code.
An inline modifier for type declarations
// Strawman syntax
// Instead of this
type Foo = Bar & Baz | Omit<Bat, "x" | "y">
// The library author can write this
inline type Foo = Bar & Baz | Omit<Bat, "x" | "y">
type Bar = { foo: "b" };
type Baz = { bar: "c" };
type Bat = { x: "x"; y: "y"; z: "z"; a: "a" };
The compiler would mostly ignore the inline directive (it would behave similarly to the Prettify helper type, however, when building the declaration files, the compiler will generate the flattened version like this
type Foo = {
foo: "b";
bar: "c";
} | {
z: "z";
a: "a";
}
Alternatively, instead of a modifier on a declaration, it can be a modifier we can add on types
type Foo = Inline<Bar> & Inline<Baz> | Inline<Omit<Bat, "x" | "y">>
Honestly, I'm not sure of the full semantics of inline. My mental intuition is based on the Prettify type helper.
type Prettify<T> = {
[K in keyof T]: T[K];
} & unknown;
Alternatively, a tsconfig.json setting might also work. Although I'm sure to have the best experience, the compiler might need some feedback from the library author too.
Sorry if this isn't fully fleshed out, but I'm willing to work on a full proposal in case the team is open to the idea.
📃 Motivating Example
We have a lot of computed types in our design system library. Most (React) components can receive Theme defined props.
Here's a verbatim example which is our Box component, which is a design system aware div.
export type BoxProps = AsChild & ThemeProps & styles.SprinklesProps & React.HTMLAttributes<HTMLDivElement>;
This describes the type perfectly, but it leads to very poor DX for the callers. When someone Cmd-Clicks Box, they see a type which isn't really helpful when trying to figure out the API of Box.
What I'd like to see in the generated declarations would be something like the following
💻 Use Cases
- What do you want to use this for?
Improving the UX of our library consumers when there are computed types in the public API. - What shortcomings exist with current approaches?
It's hard to understand the API of our library because cmputed types obscure the actual properties that are accepted by the types - What workarounds are you using in the meantime?
We wrap certain public API types with thePrettifyhelper type so that the users can hover over certain types to received a flattened version of the type
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 revisando el modificador inline propuesto, su forma alternativa a nivel de tipo y el workaround de Prettify descrito en el issue. Define la semántica para aplanar tipos calculados en archivos .d.ts generados, incluido el comportamiento de las intersecciones, las uniones y Omit. Se considerará terminado cuando haya una propuesta completa con un comportamiento claro y un camino de implementación.
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
- 25/100