∞ The example I used in my YouTube video about the `NoInfer<T>` utility added in TypeScript 5.4
type StandardUser = {
type: "standard";
};
type AdminUser = {
type: "admin";
};
type UnauthenticatedUser = {
type: "unauthed";
};
type User = StandardUser | AdminUser | UnauthenticatedUser;
export function getUser(): StandardUser | AdminUser {
return { type: "admin" };
}
export function getSession(): StandardUser | UnauthenticatedUser {
return { type: "unauthed" };
}
export const user1 = getUser();
export const user2 = getSession();
function isTypeOfUser<T extends User["type"]>(user: { type: T }, type: NoInfer<T>) {
return user.type === type;
}
isTypeOfUser(user1, "admin");
isTypeOfUser(user2, "standard");