Skip to content

type guard narrowing to a type with type with any in type parameters replaces any with unknownΒ #57892

Closed as not planned
@eps1lon

Description

@eps1lon

πŸ”Ž Search Terms

type guard parameter any unknown

πŸ•— Version & Regression Information

  • This changed between versions 4.9.6 and 5.0.4

⏯ Playground Link

https://www.typescriptlang.org/play?ts=5.5.0-dev.20240321#code/JYOwLgpgTgZghgYwgAgEIHsAeAeAKgPmQG8AoZc5ANzgBsBXCALmVxIF8SSY6QExh0IZMADOGHAQAUAIyzMeAaxDoA7iACUzWZmEi0WPIVIVkUCGDpQhYKA3acEgkWGQg6AW2nRxzcdkXKaoQAvMTU9EwAjBwkwDDIkqJ+bp7Q+JIpXlDi6urEZBSZ3lgAdOF2JgD0lSYmAHoA-PZxCUkGcCAAnulF2Vi5+Sa94mW0FRTVtRSN7EA

πŸ’» Code

interface Box<T> {
    value: T
}

function isBox<T>(box: unknown): box is Box<T> {
    return true
}

const numberBox: Box<unknown> = {value:1}

if (isBox<number>(numberBox)) {
    numberBox.value
    //        ^?
}
if (isBox<any>(numberBox)) {
    numberBox.value
    //        ^?
}

πŸ™ Actual behavior

if (isBox<any>(numberBox)) {
    numberBox.value
    //        ^? (property) Box<unknown>.value: unknown
}

πŸ™‚ Expected behavior

if (isBox<any>(numberBox)) {
    numberBox.value
    //        ^? (property) Box<any>.value: any
}

Additional information about the issue

Maybe caused by #52282 which also caused #53178

Metadata

Metadata

Assignees

No one assigned

    Labels

    Not a DefectThis behavior is one of several equally-correct options

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions