unknown
strict inequality operator
!==
type guard
unknown type with !== operator should be treated as a type guard.
Using a !== b instead of !(a === b).
function f1(x: unknown): string | undefined {
if (!(x === undefined) && typeof x !== 'string') {
throw new Error();
}
return x; // string | undefined
}
function f2(x: unknown): string | undefined {
if (x !== undefined && typeof x !== 'string') {
throw new Error();
}
return x; // currently x is `unknown`, but it could be `string | undefined`.
}
My suggestion meets these guidelines:
Fix up #33488.
Most helpful comment
Fix up #33488.