Skip to content

TypeError: Cannot read property 'charCodeAt' of undefined @ isLateBoundName #29006

Closed
@idoros

Description

@idoros

TypeScript Version: 3.3.2 (tried latest next and issue still existed)

Search Terms:
charCodeAt, isLateBoundName

Code

export interface A { type: 'a' }
export interface B { type: 'b' }
export type AB = A | B

const itemId = 'some-id'

// --- test on first level ---
const items: { [id: string]: AB } = {}
const { [itemId]: itemOk1 } = items
typeof itemOk1 // pass

// --- test on second level ---
interface ObjWithItems {
    items: {[s: string]: AB}
}
const objWithItems: ObjWithItems = { items: {}}

const itemOk2 = objWithItems.items[itemId]
typeof itemOk2 // pass

const {
    items: { [itemId]: itemWithTSError } = {} /*happens when default value is provided*/
} = objWithItems

// in order to re-produce the error, uncomment next line:
// typeof itemWithTSError // :(

// will result in:
// Error from compilation: TypeError: Cannot read property 'charCodeAt' of undefined  TypeError: Cannot read property 'charCodeAt' of undefined

Expected behavior:
No crash.

Actual behavior:
Crashes with TypeError mid-compilation:

TypeError: Cannot read property 'charCodeAt' of undefined
    at isLateBoundName (...\typescript.js:36537:25)
    at createUnionOrIntersectionProperty (...\typescript.js:37624:42)
    at getUnionOrIntersectionProperty (...\typescript.js:37690:28)
    at getPropertyOfUnionOrIntersectionType (...\typescript.js:37698:28)
    at getPropertyOfType (...\typescript.js:37731:24)
    at getTypeOfPropertyOfType (...\typescript.js:35017:24)
    at getTypeOfDestructuredProperty (...\typescript.js:44548:45)
    at getInitialTypeOfBindingElement (...\typescript.js:44613:17)
    at getInitialType (...\typescript.js:44641:17)
    at getInitialOrAssignedType (...\typescript.js:44645:17)

Playground Link:
https://www.typescriptlang.org/play/#src=export%20interface%20A%20%7B%20type%3A%20'a'%20%7D%0D%0Aexport%20interface%20B%20%7B%20type%3A%20'b'%20%7D%0D%0Aexport%20type%20AB%20%3D%20A%20%7C%20B%0D%0A%0D%0Aconst%20itemId%20%3D%20'some-id'%0D%0A%0D%0A%2F%2F%20---%20test%20on%20first%20level%20---%0D%0Aconst%20items%3A%20%7B%20%5Bid%3A%20string%5D%3A%20AB%20%7D%20%3D%20%7B%7D%0D%0Aconst%20%7B%20%5BitemId%5D%3A%20itemOk1%20%7D%20%3D%20items%0D%0Atypeof%20itemOk1%20%2F%2F%20pass%0D%0A%0D%0A%2F%2F%20---%20test%20on%20second%20level%20---%0D%0Ainterface%20ObjWithItems%20%7B%0D%0A%20%20%20%20items%3A%20%7B%5Bs%3A%20string%5D%3A%20AB%7D%0D%0A%7D%0D%0Aconst%20objWithItems%3A%20ObjWithItems%20%3D%20%7B%20items%3A%20%7B%7D%7D%0D%0A%0D%0Aconst%20itemOk2%20%3D%20objWithItems.items%5BitemId%5D%0D%0Atypeof%20itemOk2%20%2F%2F%20pass%0D%0A%0D%0Aconst%20%7B%0D%0A%20%20%20%20items%3A%20%7B%20%5BitemId%5D%3A%20itemWithTSError%20%7D%20%3D%20%7B%7D%20%2F*happens%20when%20default%20value%20is%20provided*%2F%0D%0A%7D%20%3D%20objWithItems%0D%0A%0D%0A%2F%2F%20in%20order%20to%20re-produce%20the%20error%2C%20uncomment%20next%20line%3A%0D%0A%2F%2F%20typeof%20itemWithTSError%20%2F%2F%20%3A(%0D%0A%0D%0A%2F%2F%20will%20result%20in%3A%0D%0A%2F%2F%20Error%20from%20compilation%3A%20TypeError%3A%20Cannot%20read%20property%20'charCodeAt'%20of%20undefined%20%20TypeError%3A%20Cannot%20read%20property%20'charCodeAt'%20of%20undefined

Related Issues:
Looks similar to #26321

Metadata

Metadata

Assignees

Labels

BugA bug in TypeScriptCrashFor flagging bugs which are compiler or service crashes or unclean exits, rather than bad output

Type

No type

Projects

No projects

Relationships

None yet

Development

No branches or pull requests

Issue actions