Skip to content

API panics when serializing number literal types with +Infinity and -InfinityΒ #64240

Description

@auvred

πŸ”Ž Search Terms

api, getTypeAtLocation, number literal, infinity

πŸ•— Version & Regression Information

Fails on the current main

⏯ Playground Link

No response

πŸ’» Code

  1. Create repro dir
    mkdir /tmp/repro
    cd /tmp/repro
    npm init -y
    npm add typescript@7.0.2
    echo '{}' > tsconfig.json
  2. Create files
    // index.ts
    export type Foo = 1e999
    
    // repro.mjs
    import path from 'node:path'
    import { API } from 'typescript/unstable/sync'
    
    const api = new API()
    const root = process.cwd()
    const filepath = path.join(root, 'index.ts')
    const tsconfigPath = path.join(root, 'tsconfig.json')
    const snapshot = api.updateSnapshot({ openProject: tsconfigPath })
    const project = snapshot.getProject(tsconfigPath)
    const file = project.program.getSourceFile(filepath)
    
    console.log(project.checker.getTypeAtLocation(file.statements[0].name).value)
  3. Run repro
    node repro.mjs

πŸ™ Actual behavior

> node repro.mjs
file:///private/tmp/repro/node_modules/typescript/dist/api/syncChannel.js:237
                        throw new Error(this._msgPayload.toString("utf-8"));
                              ^

Error: panic: api: failed to write response: json: cannot marshal from Go float64 within "/value": unsupported value: +Inf
goroutine 1 [running]:
runtime/debug.Stack()
        runtime/debug/stack.go:26 +0x64
github.com/microsoft/typescript-go/internal/api.(*SyncConn).handleRequest.func1()
        github.com/microsoft/typescript-go/internal/api/conn_sync.go:120 +0x44
panic({0x105b7fd60?, 0x6312dfe23c00?})
        runtime/panic.go:860 +0x12c
github.com/microsoft/typescript-go/internal/api.(*SyncConn).handleRequest(0x6312dfb08c40, {0x105e2a0b8, 0x6312dfb08b00}, 0x6312e15d9e00)
        github.com/microsoft/typescript-go/internal/api/conn_sync.go:156 +0x5c0
github.com/microsoft/typescript-go/internal/api.(*SyncConn).Run(0x6312dfb08c40, {0x105e2a0b8, 0x6312dfb08b00})
        github.com/microsoft/typescript-go/internal/api/conn_sync.go:73 +0x140
github.com/microsoft/typescript-go/internal/api.(*StdioServer).Run(0x6312dfb13dc8, {0x105e2a0b8, 0x6312dfb08b00})
        github.com/microsoft/typescript-go/internal/api/server.go:123 +0x580
main.runAPI({0x6312df93c0e0, 0x2, 0x2})
        github.com/microsoft/typescript-go/cmd/tsgo/api.go:56 +0x340
main.runMain()
        github.com/microsoft/typescript-go/cmd/tsgo/main.go:25 +0xb8
main.main()
        github.com/microsoft/typescript-go/cmd/tsgo/main.go:14 +0x1c

    at SyncRpcChannel.requestBytesSync (file:///private/tmp/repro/node_modules/typescript/dist/api/syncChannel.js:237:31)
    at SyncRpcChannel.requestSync (file:///private/tmp/repro/node_modules/typescript/dist/api/syncChannel.js:164:29)
    at Client.apiRequest (file:///private/tmp/repro/node_modules/typescript/dist/api/sync/client.js:58:37)
    at Checker.getTypeAtLocation (file:///private/tmp/repro/node_modules/typescript/dist/api/sync/api.js:750:34)
    at file:///private/tmp/repro/repro.mjs:14:29
    at ModuleJob.run (node:internal/modules/esm/module_job:439:25)
    at async node:internal/modules/esm/loader:643:26
    at async asyncRunEntryPointWithESMLoader (node:internal/modules/run_main:101:5)

Node.js v24.18.0

πŸ™‚ Expected behavior

.value is expected to be +Infinity

Additional information about the issue

No response

Activity

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

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions