X Tutup
Skip to content

Null-safe ?.toUpperCase() on Interface property is converted to :toUpperCase() (a nil value) #1218

@aSemy

Description

@aSemy

Playground example

Given some TypeScript code that calls toUpperCase on the property of an interface:

interface MyData {
    name: string | undefined
}

const myData = <MyData> {
    name : undefined
}

myData.name?.toUpperCase()

This produces Lua that fails with an error: attempt to call method 'toUpperCase' (a nil value)

myData = {name = nil}
local ____myData_name_toUpperCase_result_0 = myData.name
if ____myData_name_toUpperCase_result_0 ~= nil then
    ____myData_name_toUpperCase_result_0 = ____myData_name_toUpperCase_result_0:toUpperCase()
end

Extracting the property to a new value also produces invalid code.

const name : string | undefined = myData.name
name?.toUpperCase()
name = myData.name
local ____name_toUpperCase_result_2 = name
if ____name_toUpperCase_result_2 ~= nil then
    ____name_toUpperCase_result_2 = ____name_toUpperCase_result_2:toUpperCase()
end

But otherwise, it works

const newName: string | undefined = "blah blah blah"
newName?.toUpperCase()
-- correct
newName = "blah blah blah"
local ____newName_toUpperCase_result_4 = newName
if ____newName_toUpperCase_result_4 ~= nil then
    ____newName_toUpperCase_result_4 = string.upper(____newName_toUpperCase_result_4)
end

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

      X Tutup