{
body: {
name: 'mike',
password: 'password',
}
};
payload = req.body;
interface IUser {
name: string;
password: string;
role: 'Client' | 'Admin'
}
type UserParams = Pick<IUser, 'name' | 'password'>
const isObject = (value: unknown): value is Record<string, unknown> => {
return typeof value === 'object' && value !== null;
}
const isValidUserParams = (payload: unknown): payload is UserParams => {
return isObject(payload) &&
typeof payload['name'] === 'string' &&
typeof payload['password'] === 'string' &&
Object.keys(payload).length === 2
}
if (isValidUserParams(payload)) {
const userParams = payload as UserParams
// call user.service
} else {
// res.send(400)
}
https://t.me/ts_cool
Обсуждают сегодня