I'm currently looking to enable strict null checks in my TypeScript 2.0 project, but I'm encountering some challenges with the typings of a dependency that is nested within another dependency (referred to as the dependency grandparent).
To provide more context, I am working with dependencies B
and C
, both of which rely on A
. These dependencies are all TypeScript projects that have their code and typings stored in a lib
folder, and they have not yet been updated to support strict null checks.
The relevant typings in A
are structured as follows:
interface IInterface {
[key: string]: string;
}
These typings are utilized in both B
and C
in the following manner:
import { IInterface } from 'A/lib/iinterface';
interface IExtended extends IInterface {
myOptionalProperty?: string
}
When strict null checks are enabled, the compilation results in the following error messages:
node_modules/B/lib/extended.d.ts(4,3): error TS2411: Property 'myOptionalProperty' of type 'string | undefined' is not assignable to string index type 'string'
node_modules/C/lib/extended.d.ts(4,3): error TS2411: Property 'myOptionalProperty' of type 'string | undefined' is not assignable to string index type 'string'
This leads to a two-pronged question:
In order to meet the requirements of strict checks, the typing in
A
needs to be adjusted to:interface IInterface { [key: string]: string | undefined; }
I'm uncertain if it's feasible to override such a type, as it's not simply an expansion of existing types. If it is possible, how can this be accomplished?
If feasible, how should this inclusion be implemented to ensure that the typings in
B
andC
are validated against my customized typing, rather than the typings in their respective localnode_modules
directory?