Given a third-party TypeScript module defined like this:
// in /node_modules/third-party-module/index.d.ts
declare module 'ThirdPartyModule' {
export interface ThirdPartyInterface {
data: any;
}
}
How can I augment this module to more strictly type the data
property?
I've tried this:
// in /app/typings.d.ts
declare module 'ThirdPartyModule' {
interface IMyCustomType {}
interface ThirdPartyInterface {
// causes a compiler error: Subsequent property declarations
// must have the same type. Property 'data' must be of type
// 'any', but here has type 'IMyCustomType'.
data: IMyCustomType;
}
}
But this gives me a compiler error: "Subsequent property declarations must have the same type. Property 'data' must be of type 'any', but here has type 'IMyCustomType'."
If the third-party module instead defined the property as an actual type, like this:
// in /node_modules/third-party-module/index.d.ts
declare module 'ThirdPartyModule' {
interface IAnotherThirdPartyInterface {
something: string;
}
interface ThirdPartyInterface {
data: IAnotherThirdPartyInterface;
}
}
I could simply make my IMyCustomType
interface extend this third party type:
// in /app/typings.d.ts
declare module 'ThirdPartyModule' {
interface IMyCustomType extends IAnotherThirdPartyInterface {}
interface ThirdPartyInterface {
data: IMyCustomType;
}
}
However, since the type is defined any
, I can't extend it:
// causes a compiler error: 'any' only refers to a type,
// but is being used as a value here.
interface IMyCustomType extends any {}
request.app
property. The property is a generic place to store application data, so it is typed asany
by the library's authors. However, since I know how this property will be used, I'd like to add typing information to this property to avoid casts. – Feme