Quiero pasar accesorios de forma selectiva al componente de la biblioteca.
La biblioteca ha creado un tipo en línea mediante la codificación de varios valores.
Me interesa que mi código se actualice automáticamente con adiciones al tipo definido en la biblioteca. ¿Puedo reutilizar esos valores en línea sin volver a crearlos?
Dentro de la biblioteca --> el archivo componente.d.ts tiene
export interface TooltipProps { ...manyOtherProps, placement?: | 'bottom-end' | 'bottom-start' | 'bottom' | 'left-end' | 'left-start' | 'left' | 'right-end' | 'right-start' | 'right' | 'top-end' | 'top-start' | 'top'; }Probé algunos intentos con keyof y valueof
type ValueOf<T> = T[keyof T]; type PlacementStrings = ValueOf<TooltipProps> interface JustPlacement { placement: "top" } type PlacementStringsKeys = keyof TooltipProps & keyof JustPlacement type stpls = ValueOf<PlacementStringsKeys>Pero no obtener lo necesario.
que seria sin reescribirlo
interface CustomTooltipProps { placement?: | 'bottom-end' | 'bottom-start' | 'bottom' | 'left-end' | 'left-start' | 'left' | 'right-end' | 'right-start' | 'right' | 'top-end' | 'top-start' | 'top'; }Puede usar el Pick de selección para crear un nuevo tipo de objeto que incluya todas las claves/valores donde la clave se puede asignar al tipo especificado:
// mylib.ts export interface TooltipProps { foo: "bar"; placement?: | 'bottom-end' | 'bottom-start' | 'bottom' | 'left-end' | 'left-start' | 'left' | 'right-end' | 'right-start' | 'right' | 'top-end' | 'top-start' | 'top'; } // otherfile.ts import type { TooltipProps } from "./mylib"; type CustomTooltipProps = Pick<TooltipProps, "placement">; // this is expanded into the following: type CustomTooltipProps = { placement?: "bottom-end" | "bottom-start" | "bottom" | "left-end" | "left-start" | "left" | "right-end" | "right-start" | "right" | "top-end" | "top-start" | "top" | undefined; }