In the following example, I seek to describe a complex typescript type that I want to use later FinalType. The thing is, due to it's complexity, this type required the declaration of intermediary types that are polluting the page IntermediaryType/IntermediaryType2.
type IntermediaryType = {
decisiveKey: true,
mutatedKey: (params: any) => void,
} | {
decisiveKey: false,
mutatedKey?: false,
}
interface IntermediaryType2 {
foo?: string,
bar?: boolean,
}
type FinalType = IntermediaryType & IntermediaryType2;
const Foo = (param: FinalType) => {}
Foo({
decisiveKey: true,
mutatedKey: () => {},
});
My question is, is there any way to makes the intermediary types unreachable, and only allow the use of FinalType ?
I've seen that you can enclosure some part of the code using brackets like :
{
type IntermediaryType = {
decisiveKey: true,
mutatedKey: (params: any) => void,
} | {
decisiveKey: false,
mutatedKey?: false,
}
interface IntermediaryType2 {
foo?: string,
bar?: boolean,
}
type FinalType = IntermediaryType & IntermediaryType2;
}
const Foo = (param: FinalType) => {}
Foo({
decisiveKey: true,
mutatedKey: () => {},
});
But then I obviously can't access FinalType. I've tried to use return or export but none works.
The ideal would be something like :
type FinalType = {
type IntermediaryType = {
decisiveKey: true,
mutatedKey: (params: any) => void,
} | {
decisiveKey: false,
mutatedKey?: false,
}
interface IntermediaryType2 {
foo?: string,
bar?: boolean,
}
return IntermediaryType & IntermediaryType2;
}
const Foo = (param: FinalType) => {}
Foo({
decisiveKey: true,
mutatedKey: () => {},
});
Any leads ?
Using @Aluan Haddad answer, for now the best I can achieve is :
namespace _ {
type IntermediaryType = {
decisiveKey: true,
mutatedKey: (params: any) => void,
} | {
decisiveKey: false,
mutatedKey?: false,
}
interface IntermediaryType2 {
foo?: string,
bar?: boolean,
}
export type FinalType = IntermediaryType & IntermediaryType2;
}; type FinalType = _.FinalType;
const Foo = (param: FinalType) => {}
Foo({
decisiveKey: true,
mutatedKey: () => {},
});
I would love to add some syntax sugar on it!