Closed as not planned
Description
🔎 Search Terms
"null checking", "function inference", "null check inference", "function null check"
🕗 Version & Regression Information
- This is a crash
- This changed between versions ______ and _______
- This changed in commit or PR _______
- This is the behavior in every version I tried, and I reviewed the FAQ for entries about function inference, null check
- I was unable to test this on prior versions because _______
⏯ Playground Link
💻 Code
// Your code here
interface MyInterface {
id: string
nullable: string | null
}
function failedInference() {
const input: MyInterface = {
id: 'id',
nullable: 'non-null'
}
if (!input.nullable) {
throw new Error()
}
return input
}
const testFailedInference = failedInference()
// This shouldn't fail because the function validates that the nullable field is non-nullable but it fails
let nullableLength = testFailedInference.nullable.length
🙁 Actual behavior
The type of the returned testFailedInference.nullable
is still string | null
and this is wrong because I already validated it to be non nullable in the function and typescript is inferring function return type
🙂 Expected behavior
The type of the returned testFailedInference.nullable
is just string
and not nullable.
Additional information about the issue
Implementing it in a form-factor like this works
function successInference() {
const input: MyInterface = {
id: 'id',
nullable: 'non-null'
}
if (!input.nullable) {
throw new Error()
}
return {
...input,
nullable: input.nullable
}
}
const testSuccessInference = successInference()
// This succeeds after manually specifying in the function return
let nullable2Length = testSuccessInference.nullable.length