у меня есть перечисление
export const trackingKeys = {
Form: 'form',
Video: 'video',
} as const
У меня есть тип, который дает для ключа свойство типа
export type TrackingPropertiesByKey = {
[trackingKeys.Form]: { bar : number }
[trackingKeys.Video]: { foo : boolean }
}
И функция отслеживания
export type TrackingEventKey =
typeof trackingKeys[keyof typeof trackingKeys]
const track = <T extends TrackingEventKey>(
event: TrackingEventKey,
properties?: TrackingPropertiesByKey[T]
) => trackFromLib(event, properties)
Использование track(trackingKeys.Form, {foo : true, bar: 1 }) не дает мне ошибки.
У меня есть следующий тип:
const track: <TrackingEventKey>(event: TrackingEventKey, properties?: {
bar: number;
} | {
foo: boolean;
}) => void
Я не хочу properties?: {bar: number;}|{foo: boolean;} но в данном случае { bar : number }






Вы также должны определить связь между параметром event и T:
const track = <T extends TrackingEventKey>(
event: T,
properties?: TrackingPropertiesByKey[T]
) => trackFromLib(event, properties)
Вы указали тип event как TrackingEventKey, но TypeScript должен знать, что event определяет тип T, чтобы сузить тип.
Спасибо @tobias-s !! Я нашел ответ с вашей помощью! я отредактирую свой вопрос