Skip to content

Array indexing does not get +1 in transpiled output in a rather specific scenario with optional chaining #1605

@GlassBricks

Description

@GlassBricks

When all:

  • Foo is an interface from extending Array or ReadonlyArray
  • An object bar has a property with type Foo | undefined
  • Optional chaining used to array-index Foo
  • ...after a property access to bar

Then, the transpiled lua does not get +1 on the array indexing.

interface Foo extends Array<number> {}
const b: {arr?: Foo} = {arr:[1,2]}

// incorrectly prints 1
print(b.arr?.[1])

const arr: Foo | undefined = b.arr
print(arr?.[1])

// all correctly prints 2
print(b.arr![1])

const arr2 : Foo | undefined = [1,2]
print(arr2?.[1])

const c: {arr?: number[]} = {arr:[1,2]}
print(c.arr?.[1])

const d: {arr: Foo} = {arr:[1,2]}
print(c.arr?.[1])

TSTL v1.28.1

I'm amazed and annoyed how many stars had to align to encounter this bug, and that it can't be more miminal.

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions