무슨 뜻인지 어떻게 설명해야할지 모르겠지만 이것이 제가 가진 것입니다.
enum ContactBaseType {
Email,
Notification
}
interface ContactBase {
[ContactBaseType.Email]: EmailType[];
[ContactBaseType.Notification]: NotificationType[];
}
내가하고 싶은 것 :
function ToggleSubscription(lookup: ContactBase, type: ContactBaseType, enumType: EmailType | NotificationType) {
// something like this, however this gives me a "EmailType | NotificationType is not assignable to never"
const index = lookup[type].indexOf(enumType);
}
몇 가지 다른 솔루션을 시도했지만 작동하지 않는 것 같아서 뭔가 빠진 것 같습니다. 다음과 같은지도를 사용해 보았습니다.
type ContactBaseFlags = ContactBaseType.Email | ContactBaseType.Notification;
type ContactBaseMap = { [K in ContactBaseFlags]: K extends ContactBaseType.Email ? EmailType[] : NotificationType[] };
// or
type ContactBaseMap: { [K in ContactBaseFlags]: EmailType[] | NotificationType[] }
keyof 등을 사용하여 키 추출을 시도했습니다.
그런 걸 해봐
function ToggleSubscription<
B extends ContactBase & {[key in T]: Array<I>}, // <- our base object
T extends keyof B, // <- one of its keys
I extends B[T] extends Array<infer U> ? U : never, // <- type of a related array
>(lookup: B, type: T, enumType: I) {
// works now.
const index = lookup[type].indexOf(enumType);
}
이 기사는 인터넷에서 수집됩니다. 재 인쇄 할 때 출처를 알려주십시오.
침해가 발생한 경우 연락 주시기 바랍니다[email protected] 삭제
몇 마디 만하겠습니다