My initial idea was something along these lines:
type Foo<T, K extends string> = K extends "isDirty"
? never
: {
[P in K]: T;
isDirty: boolean;
};
However, Typescript is still unaware that K
will never be `"isDirty"`.
What I intended to achieve was the ability to accept any key value, such as foo
, bar
, potatoes
, and have it of type T
. Additionally, I wanted to include the property isDirty: boolean
, making sure that only K
cannot have that specific value.
Here are some examples:
const foo: Foo<number, "bar"> = {
bar: 5,
isDirty: false
}
const potatoes: Foo<number[], "potatoes"> = {
potatoes: [1,6,15],
isDirty: false
}
Is achieving this possible in Typescript?