我如何写这句话以达到与下面示例相同的结果,从而使我可以选择两个密钥?
export interface RTCPeerExchange {
from: string;
[offer|answer]: RTCSessionDescriptionInit;
}
我知道我可以使用一种类型来实现这一目标,但是有没有一种方法可以使用接口实现相同的方法?
type RTCPeerExchange = {
from: string;
answer: RTCSessionDescriptionInit;
}|{
offer: RTCSessionDescriptionInit;
from: string;
}
interface RTCPeerExchange<T extends "offer" | "answer"> {
from: string;
type: T;
data: T extends "offer" ? RTCSessionDescriptionInit : RTCSessionDescriptionInit;
}
检查是否有帮助。
,但很酷的想法也许您可以向社区请求: