Loading...
function isUser(value: any): value is User {
return 'id' in value;
}function isUser(value: unknown): value is User {
return (
typeof value === 'object' &&
value !== null &&
'id' in value &&
typeof value.id === 'string'
);
}unknown forces real checks; the any version throws on null and accepts any id.
true for the wrong shape, and TypeScript trusts it without warning.value is T, and the body really checks what it claims.null narrow the result, e.g. filter((x): x is User => x !== null).unknown input field by field, not by casting the value.A boolean return does not narrow at the call site. value is T narrows the argument in the if branch.
Yes, for simple checks such as x => x !== undefined, so filter() can narrow without an annotation.