Can you take a look at this code. Why Typescript is complaining about the code inside the first “if” statement, and not complaining about the code in the second one?
export type IssueProps = {
type: 'job_warning' | 'job_error' | 'task_warning'
jobId?: string
}
export type JobIssueProps = IssueProps & {
type: 'job_warning' | 'job_error'
jobId: string
}
const sendDeadlineJobErrorAlert = (
deadlineIssue: JobIssueProps,
) => {
console.log(deadlineIssue)
}
const issue = {} as IssueProps
if (issue.type === 'job_error' && issue.jobId) {
// typescript reports an error
sendDeadlineJobErrorAlert(issue)
}
if (issue.type === 'job_error' && issue.jobId) {
// typescript doesn't report an error
sendDeadlineJobErrorAlert({...issue, type: issue.type, jobId: issue.jobId})
}
Playground Link