-
Notifications
You must be signed in to change notification settings - Fork 13k
Closed
Description
π Search Terms
Type narrowing, type narrowing with conditionals, type not narrowed after check
π Version & Regression Information
This is the behavior in every version I tried, and I reviewed the FAQ for entries about type narrowing
β― Playground Link
π» Code
type MaybeHasInformation = {
email?: string,
name?: {
first?: string,
last?: string,
}
}
type HasEmailAndIfItHasNameItHasFirstName = {
email: string,
name?: {
first: string,
last?: string,
}
}
function transform(from: MaybeHasInformation): HasEmailAndIfItHasNameItHasFirstName {
const email = from.email;
if (email === undefined) {
throw new Error('NOPE');
}
return from.name?.first
? {email, name: from.name}
: {email};
}```
### π Actual behavior
There is a compilation error:
Type '{ email: string; name: { first?: string | undefined; last?: string | undefined; }; } | { email: string; }' is not assignable to type 'HasEmailAndIfItHasNameItHasFirstName'.
Type '{ email: string; name: { first?: string | undefined; last?: string | undefined; }; }' is not assignable to type 'HasEmailAndIfItHasNameItHasFirstName'.
The types of 'name.first' are incompatible between these types.
Type 'string | undefined' is not assignable to type 'string'.
Type 'undefined' is not assignable to type 'string'.
### π Expected behavior
No error was expected.
### Additional information about the issue
_No response_
Metadata
Metadata
Assignees
Labels
No labels