Skip to content

Naming conflict ends up in unhandled compiler error #38532

Closed
@timsuchanek

Description

@timsuchanek

TypeScript Version: 3.7.x-dev.201xxxxx

Search Terms:

Code
It seems, that the combination of importing Debug, redefining it as a function and then setting a property on that function (Debug.enable) breaks the compiler.

import Debug from 'debug'

const cache: any[] = []

const MAX_LOGS = 100

const namespaces: string[] = []

// for our use-case, map is faster, as retrieving values is faster than with Set or Object
const enabledNamespaces: Map<string, true> = new Map<string, true>()

export default function Debug(namespace: string): Debug.Debugger {
  const debug: any = Debug(namespace)
  namespaces.push(namespace)

  Debug.enable(namespaces.join(','))

  debug.log = (...args) => {
    cache.push(args)
    // keeping 100 logs is just a heuristic. The real truncating comes later
    if (cache.length > MAX_LOGS) {
      cache.shift()
    }
    if (enabledNamespaces.has(namespace)) {
      console.error(...args)
    }
  }

  return debug
}

Debug.enable = (namespace) => {
  enabledNamespaces.set(namespace, true)
}

Expected behavior: The compiler should give me a readable error message, that I have a naming conflict.

Actual behavior:

errors.ts:22 Uncaught Error: Cannot read property 'kind' of undefined

TypeError: Cannot read property 'kind' of undefined
    at Object.e.isFunctionDeclaration (tsWorker.js:21)
    at Object.$n [as getEffectiveTypeAnnotationNode] (tsWorker.js:21)
    at tsWorker.js:21
    at Vm (tsWorker.js:21)
    at Qm (tsWorker.js:21)
    at Jm (tsWorker.js:21)
    at a_ (tsWorker.js:21)
    at tsWorker.js:21
    at Gy (tsWorker.js:21)
    at tsWorker.js:21
    at errors.ts:22

Playground Link

Metadata

Metadata

Assignees

Labels

BugA bug in TypeScript

Type

No type

Projects

No projects

Relationships

None yet

Development

No branches or pull requests

Issue actions