There is single interface IStorage where every key has two types:
interface IStorage { key: boolean | CustomStore<boolean>, key2: number | CustomStore<number> } type CustomStore<T> = { value: T, init: (value: T) => void; set: (value: T) => void; } The class LiveStorage have defaultStorage (first type of IStorage) and storage (second):
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 } } When create instance, key has both types:
let liveStorage = new LiveStorage<IStorage, IStorage>({}) liveStorage.storage.key // boolean | CustomStore<boolean> Are there ways to key will have only CustomStore<boolean> by default? With single interface and without (liveStorage.storage.key as CustomStore<boolean>). I know that is only compile time, just want to get suggestion from IDE without (x as type) every time.