microsoft/typescript-go · error

failed to unmarshal %T: %w

Error message

failed to unmarshal %T: %w

What it means

The request's params JSON failed to unmarshal into the strongly-typed params struct registered for that method (unmarshallerFor[T]). The message prints the Go type name and the underlying json error, which names the offending field and reason.

Source

Thrown at internal/api/proto.go:1462

	result := make([]*DiagnosticResponse, len(diags))
	for i, d := range diags {
		result[i] = NewDiagnosticResponse(d)
	}
	return result
}

func unmarshalPayload(method string, payload json.Value) (any, error) {
	unmarshaler, ok := unmarshalers[Method(method)]
	if !ok {
		return nil, fmt.Errorf("unknown API method %q", method)
	}
	return unmarshaler(payload)
}

func unmarshallerFor[T any](data []byte) (any, error) {
	var v T
	if err := json.Unmarshal(data, &v); err != nil {
		return nil, fmt.Errorf("failed to unmarshal %T: %w", (*T)(nil), err)
	}
	return &v, nil
}

func noParams(data []byte) (any, error) {
	return nil, nil
}

View on GitHub (pinned to 1bcfa18d79)

Solutions

  1. Match the client's types to the server's params struct for that method in proto.go
  2. Validate the payload against the expected shape before sending
  3. Fix the specific field named in the wrapped json error
  4. Add contract tests that round-trip params through JSON

Example fix

// before
{ "file": "/a.ts", "line": "7" } // line as string

// after
{ "file": "/a.ts", "line": 7 }
Defensive patterns

Strategy: validation

Validate before calling

// Prove the payload marshals into the expected shape before sending.
if _, err := json.Marshal(params); err != nil {
	return fmt.Errorf("params not serializable: %w", err)
}
// During development, round-trip against the server's params type:
var want api.GetDiagnosticsParams
if b, _ := json.Marshal(params); json.Unmarshal(b, &want) != nil {
	return errors.New("params do not match server schema")
}

Prevention

When it happens

Trigger: Wrong field types (string where a number is expected), missing required fields, extra nesting levels, malformed values inside nested structures.

Common situations: Schema drift between client models and server structs after an upgrade; hand-written JSON; numeric fields sent as quoted strings; enum fields with unknown values.

Related errors


AI-assisted analysis of microsoft/typescript-go@1bcfa18d79 (2026-08-16). Data as JSON: /api/errors/3a996277fcbfb8b8. Report an issue: GitHub.