Skip to content

Null checks returned from function fails to return accurate types #59238

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
livingforjesus opened this issue Jul 11, 2024 · 2 comments
Closed
Labels
Working as Intended The behavior described is the intended behavior; this is not a bug

Comments

@livingforjesus
Copy link

🔎 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

https://www.typescriptlang.org/play/?#code/JYOwLgpgTgZghgYwgAgLIE8CS5rycgbwChllgATALmQGcwpQBzE5EAVwBsO4AjDianQYhGyAD6tOHIgF8iRGGxAIwwAPYhk8YP3LYY0CMogAKAJSEWCDXTIgADmzDUM2SLEQoAvJdKkK1ADkFIEANCyk7Fy8-EEgGgC0URyBLHL+MMgmAISgjmAAdMkxEBbEfshgABZQagDurBANAKJQtVDmafKkUBBgbFCaeU6y8tYgtpB0AGJwOhB6IAa9xsg+2rr6hsadAPS7yAAqVcA0tFVqnOQggWBacxzIPBAIcGw0KNUoisqqGsgANzgHAocCmlSqYIhKGKfG+wAgHHIZDO8RASSkJSeTjIdw2NCI-DusP4ABkjIxqmtKhAZg8FlsVkgipi4QV+CJqkA

💻 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
@MartinJohns
Copy link
Contributor

MartinJohns commented Jul 11, 2024

This is working as intended. Checks within functions do not propagate to outside the function, unless you're writing an explicit type guard or assertion function. Your function returns a MyInterface, and the nullable property can be null.

See also #42384.

@RyanCavanaugh RyanCavanaugh added the Working as Intended The behavior described is the intended behavior; this is not a bug label Jul 11, 2024
@typescript-bot
Copy link
Collaborator

This issue has been marked as "Working as Intended" and has seen no recent activity. It has been automatically closed for house-keeping purposes.

@typescript-bot typescript-bot closed this as not planned Won't fix, can't repro, duplicate, stale Jul 14, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Working as Intended The behavior described is the intended behavior; this is not a bug
Projects
None yet
Development

No branches or pull requests

4 participants