tengo la siguiente interfaz
export interface AuctionMOLQueryParams { filterAuctionDirection?: string; } porque filterAuctionDirection puede ser uno de los tres valores posibles que configuré para ser enum
export enum ProductDirection { upwards = 'A01', downwards = 'A02', all = 'All' } export interface AuctionMOLQueryParams { filterAuctionDirection?: ProductDirection; }el problema es que ahora cuando trato de usar este tipo de objeto
let queryParams: AuctionMOLQueryParams = { filterAuctionDirection: ProductDirection.all, }Los tipos de propiedad 'filterAuctionDirection' son incompatibles. El tipo 'cadena' no se puede asignar al texto fuerte de tipo 'ProductDirection'
Como puedo resolver esto ? ¿Cómo puedo mapear mi filterAuctionDirection para que sea uno de estos tres valores y el tipo de seguridad seguirá funcionando correctamente?
TS2717: Subsequent property declarations must have the same type. Property 'filterAuctionDirection' must be of type 'string', but here has type 'ProductDirection'. Si entendí su pregunta correctamente, está definiendo la interfaz AuctionMOLQueryParams dos veces.
Inicialmente, define el campo filterAuctionDirection como una string , pero luego intenta marcarlo como ProductDirection .
Si está intentando restringir un tipo de variable de una interfaz declarada externamente (en este ejemplo AuctionMOLQueryParams ), debe definir una nueva interfaz que se extienda.
Ejemplo para su caso de uso:
export interface AuctionMOLQueryParams { filterAuctionDirection?: string; } export enum ProductDirection { upwards = 'A01', downwards = 'A02', all = 'All' } export interface MyAuctionMOLQueryParams extends AuctionMOLQueryParams { filterAuctionDirection?: ProductDirection; } let queryParamsValid1: MyAuctionMOLQueryParams = { filterAuctionDirection: ProductDirection.all, } let queryParamsInvalid2: MyAuctionMOLQueryParams = { filterAuctionDirection: "My", } let queryParamsInvalid1: MyAuctionMOLQueryParams = { filterAuctionDirection: "All", }