I want to create an interface whereby I can extend any typescript type to add a property to extend a generic type. For example, create a Confidence<any>
type that looks like this:
export interface Confidence<T> extends T{
confidenceLevel:number
}
It can then be accessed by going:
const date:Confidence<Date> = new Date();
date.confidenceLevel = .9;
This does not seem to be possible (and I get the feeling it is anti-pattern), however I can do
export type Confidence<T> = T & {
confidenceLevel:number
}
Which appears to accomplish what I want to do though I feel like I'm cheating doing this.
I recognize this "reverse extension" could be problematic if I overwrite a property of the generic type, but is that the only concern? I'm having trouble wrapping my head around this, what is the best way to create a type that merely adds a property?