Many years I had been used following code
export interface User {
readonly name: User.Name;
readonly address: User.Address;
}
export namespace User {
export interface Name {
readonly first: string;
readonly last: string;
}
export interface Address {
readonly country: string;
readonly city: string;
}
}
I like Name
and Address
interfaces accessible via User
interface, that helps to prevent name conflicts, show relation, etc.
Right now, I have a fight with ESLint rule @typescript-eslint/no-namespace
Is it possible to achieve same typing without violation of the rule?
There is no way to use a namespace without violating the lint rule.
However you could do-away with the namespace entirely by inlining your types:
export interface User {
readonly name: {
readonly first: string;
readonly last: string;
};
readonly address: {
readonly country: string;
readonly city: string;
};
}
If you want to specifically reference the address or name type later in code you can then do so using an indexed type reference:
type UserName = User['name'];
type UserAddress = User['address'];
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.