存在单个接口IStorage
,其中每个键具有两种类型:
interface IStorage {
key: boolean | CustomStore<boolean>,
key2: number | CustomStore<number>
}
type CustomStore<T> = {
value: T,
init: (value: T) => void;
set: (value: T) => void;
}
班级LiveStorage
有defaultStorage
个(第一类IStorage
)和storage
个(第二类):
class LiveStorage<D = { [key: string]: any }, S = { [key: string]: CustomStore<any> }> {
private defaultStorage: D
storage: S
constructor(defaultStorage: D) {
this.defaultStorage = defaultStorage as D
this.storage = {} as S
}
}
创建实例时,key
同时具有这两种类型:
let liveStorage = new LiveStorage<IStorage, IStorage>({})
liveStorage.storage.key // boolean | CustomStore<boolean>
有没有办法让key
默认只有CustomStore<boolean>
?具有单一接口,并且没有(liveStorage.storage.key as CustomStore<boolean>)
.我知道那只是编译时间,每次都想从没有(x as type)
的IDE那里得到建议.