I am working on a project where I need to enhance a typescript type by adding a new property in order to extend a generic type. To achieve this, I plan to define a Confidence<any>
type that has the following structure:
export interface Confidence<T> extends T {
confidenceLevel: number
}
To utilize this new type, I can do the following:
const date: Confidence<Date> = new Date();
date.confidenceLevel = 0.9;
Although it seems challenging (and possibly not recommended), I have found an alternative approach:
export type Confidence<T> = T & {
confidenceLevel: number
}
This second method achieves the desired outcome, but I feel uncertain about its validity. While I acknowledge that overriding properties of the generic type could present challenges, is there anything else I should be aware of? I am struggling to fully understand this concept - what would be the most effective way to create a type that simply adds a property?