Skip to content
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

'Never': different behavior with and without function type declaration #57084

Closed
HironTez opened this issue Jan 18, 2024 · 1 comment
Closed
Labels
Duplicate An existing issue was already created

Comments

@HironTez
Copy link

HironTez commented Jan 18, 2024

πŸ”Ž Search Terms

Never, return type never

πŸ•— Version & Regression Information

  • This is the behavior in every version I tried

⏯ Playground Link

https://www.typescriptlang.org/play?#code/DYUwLgBAhgXBDOYBOBLAdgcwgHwgVzQBMQAzdEQgKFEgCM5FVMd8jTyqaIBjB5dLLgLEyaCpUrcA9mkTQSEALwQAFAEolAPggBvShAMQwACyRSA7hDGWAokjNJ1lAL6SZc2guXq4YgG4gSFq6+oYmZpbWEHYOTq7SspDcJHDqwf6BSqoaitp6hkamFlYgtvZSjmouEmAAngAOIEYgiACCAGJZAErgeEhoACoNIAA8dY1SClAkmpQA9HOGAHoA-JTjTWAtYABCnco9YH2Dw2PDkxCeswvLaxvNiADC+xCHx0ONZxMKydeLBqsJJQUAoVABCKAaabqADclCgADpQJgTBIQaowbQNJ5YZRaEiQCjjBA0aCwdwNMlcdwCUSIEA

πŸ’» Code

let a: string | undefined
let b: string | undefined
let c: string | undefined

const af = () => {
    throw new Error()
}
const bf = (): never => {
    throw new Error()
}
const cf: () => never = () => {
    throw new Error()
}

if (!a) af();
a.length // Error

if (!b) bf();
b.length // Error

if (!c) cf();
c.length // Fine

πŸ™ Actual behavior

Typescript does not check 'never' in condition statements but does if the whole function is declared

πŸ™‚ Expected behavior

The 'never' return type should always behave in the same way

Additional information about the issue

No response

@MartinJohns
Copy link
Contributor

This is working as intended. See #32695 and many many others (e.g. #55351).

@RyanCavanaugh RyanCavanaugh added the Duplicate An existing issue was already created label Jan 18, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Duplicate An existing issue was already created
Projects
None yet
Development

No branches or pull requests

3 participants