Is there a way to force excess-property checking to happen, not just for an inlined object literal but one that derives from a variable?
For example, suppos
I needed this to enforce an object shape in Redux.
I have combined the answer in this great article and Shannon's answer in this thread here. I think this gives a tiny bit more concise way to implement this:
export type StrictPropertyCheck = T extends TExpected
? Exclude extends never
? T
: TError
: TExpected
Here ^^ I put the T extends TExpected
in the StrictPropertyCheck
, that is all the difference actually but I thought linking the article above would help others landing on this thread.
export type Credentials = {
email: string
password: string
}
export type StrictCreds = T &
StrictPropertyCheck<
T,
Credentials,
'ERROR: THERE ARE EXCESS PROPERTIES IN CREDENTIALS OBJECT'
>
export type AuthActionType =
| {
type: AuthAction.LOGIN
payload: StrictCreds
}
| { type: AuthAction.LOGOUT
export const login = (credentials: StrictCreds): AuthActionType => {
return {
type: AuthAction.LOGIN,
payload: credentials as Credentials,
}
}