Skip to content

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

Closed
@HironTez

Description

@HironTez

πŸ”Ž 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

Metadata

Metadata

Assignees

No one assigned

    Labels

    DuplicateAn existing issue was already created

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions