Skip to content

Parsing bug for foo<typeof a[0].b>()Β #44857

Closed

Description

Bug Report

πŸ”Ž Search Terms

Parsing, typeof, generic, index access

πŸ•— Version & Regression Information

Tested with 4.3.4

⏯ Playground Link

Playground

πŸ’» Code

let a = [{ b: 1 }] as const
let foo = <T extends 1>() => ""

foo<typeof a[0].b>();
// Unexpected error:
// Operator '<' cannot be applied to types '<T extends 1>() => string' and 'string'. (2365)

let z = a[0].b
foo<typeof z>()

πŸ™ Actual behavior

There is an error at line 4 - Operator '<' cannot be applied to types '<T extends 1>() => string' and 'string'. (2365)

πŸ™‚ Expected behavior

There should be no errors.

Activity

  1. whzx5byb commented on Jul 1, 2021

    @whzx5byb

    In your example, the typeof operator appears in a type context (not expression context!), and you should use indexed access type foo<typeof a[0]['b']>() to get the type of a property.

  2. devanshj commented on Jul 1, 2021

    @devanshj
    Author

    Umm I don't think so, if it were in a type context typeof a.b.c would also not work but it does.

  3. DanielRosenwasser commented on Jul 1, 2021

    @DanielRosenwasser
    Member

    Seems possibly related to #19707.

    My understanding is that we try parsing type arguments, we see typeof a[0].b which is initially parsed as (typeof a)[0]. The . can't follow the type though because the left side is not valid for a qualified name though, so we determine that we can't parse the type arguments and throw the work away, back-tracking and trying to parse it as a < comparison.

  4. sandersn commented on Jul 2, 2021

    @sandersn
    Member

    I agree, this is a duplicate of #19707

  5. locked as resolved and limited conversation to collaborators on Oct 21, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

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