I have to overload a type to forbidden some properties, so I cannot update the original type, and must create a new one. I could create a new type but that must accept every properties EXCEPT some.
type Foo = {
[key: string]: any
}
type ForbiddenProperties = "a";
type Test = Foo & { [key in ForbiddenProperties]: never } // AtMostOneOf<Foo>;
const test: Test = {a: 1}; // Must NOT Work
const test1: Test = {b: 'asd'}; // Must Work
const test2: Test = {a: 1, b: 'asd'}; // Must NOT Work
const test3: Test = {} // Must Work
The Foo
type is the original, and the Test
is the new one.
Of course if the Foo
would be easier like
type Foo = {
a: string,
b: string,
}
It would work fine, but not, the index signatures return errors everytime.
If the a
property exist, the error is fine, but if there no forbiddenProperty, typescript ask me to ADD the missing property
Type '{}' is not assignable to type 'Test'.
Property 'a' is missing in type '{}' but required in type '{ a: never; }'.
To play with it, there is a (playground)[https://www.staging-typescript.org/play?jsx=0&ts=4.3.5#code/C4TwDgpgBAYg9nKBeKBvAUFLUDaBrCEALigGdgAnASwDsBzAXRIEMaR0BfddUSWOCgCMqAExEQaABQpxIFYFQilkUAETNVAbm69oAFSXAV8RADI0uAiCi1+Q0eKky5CpUyg0IANwgUoHLAB6QKgAQWAAWThyAHlPGIAzAB4TAD5tdABjOBpyKGBDEgM8lFRmEgBGDk0oYKgIgFc8gDkYvSgAdQE8LJy8gvIKosMVVEESAHJmUhEJ6tqQxryuih7s3KMB4AAmYZK0cqgKgBoocagpmbmauqWjVvaVtb7NwwBmPaNSgNumoyfuOggA ]
I try a lot of things, like exclude explicitly
type Foo = {
[key: Exclude<string, 'a'>]: string
}
But all properties, even the forbidden one
If you have an idea.