Skip to content

Fix error message truncation in TypeScript validator#325

Merged
robgruen merged 4 commits intomainfrom
copilot/investigate-error-message-truncation
May 4, 2026
Merged

Fix error message truncation in TypeScript validator#325
robgruen merged 4 commits intomainfrom
copilot/investigate-error-message-truncation

Conversation

Copy link
Copy Markdown
Contributor

Copilot AI commented May 4, 2026

TypeScript diagnostic code 2740 (triggered when 6+ required properties are missing) hardcodes props.slice(0, 4) internally, producing truncated messages like:

Type '{ id: number; title: string; }' is missing the following properties from type 'Doc': slug, abstract, description, and 4 more.

Additionally, the original code extracted only the top-level .messageText from a DiagnosticMessageChain, silently dropping any nested detail in the .next chain.

Changes (typescript/src/ts/validate.ts)

  • DiagnosticMessageChain handling: Replace manual d.messageText.messageText extraction with ts.flattenDiagnosticMessageText(d.messageText, "\n"), which correctly traverses the full chain.

  • expandMissingPropertiesMessage helper: For diagnostic code 2740, uses program.getTypeChecker() to reconstruct the complete missing-property list. Locates the exact variable declaration via decl.getStart(file) <= d.start <= decl.end, filters optional symbols (ts.SymbolFlags.Optional), and formats with ts.TypeFormatFlags.NoTruncation. Falls back to the original compiler message if expansion fails.

// Before
Type '{ id: number; title: string; }' is missing the following properties
from type 'Doc': slug, abstract, description, and 4 more.

// After
Type '{ id: number; title: string; }' is missing the following properties
from type 'Doc': slug, abstract, description, author, date, category, content, tags

Note: noErrorTruncation compiler option does not address this — it only affects type name length, not the property list count.

Tests (typescript/test/validate.test.ts)

Added 12 unit tests using Node.js's built-in node:test / node:assert (no new dependencies), runnable via npm test. The tests verify:

  • Valid objects continue to pass validation (backward-compatible)
  • Type-mismatch errors (TS codes other than 2740) are unaffected
  • Single missing property (TS 2741) and 2–5 missing properties (TS 2739) are unaffected
  • 6+ missing required properties (TS 2740) now returns the full property list with no "and N more" truncation
  • Optional properties are correctly excluded from the missing-properties list

Copilot AI and others added 2 commits May 4, 2026 18:49
Two improvements to diagnostic message handling in createTypeScriptJsonValidator:

1. Use ts.flattenDiagnosticMessageText() instead of manually extracting
   d.messageText.messageText to properly handle DiagnosticMessageChain
   messages with nested detail.

2. For TypeScript error 2740 (missing properties truncated with "and N
   more"), use the type checker to reconstruct the full list of missing
   required properties rather than relying on the compiler's hardcoded
   4-property limit.

Agent-Logs-Url: https://github.com/microsoft/TypeChat/sessions/bc17d936-9d71-49f6-9fe7-d0a6d9d46770

Co-authored-by: robgruen <25374553+robgruen@users.noreply.github.com>
- Use decl.getStart(file) instead of decl.pos to exclude leading trivia
  from the position range check, making it more accurate
- Add a JSDoc comment explaining the function's purpose and return value

Agent-Logs-Url: https://github.com/microsoft/TypeChat/sessions/bc17d936-9d71-49f6-9fe7-d0a6d9d46770

Co-authored-by: robgruen <25374553+robgruen@users.noreply.github.com>
Copilot AI changed the title [WIP] Investigate root cause of error message truncation in TypeScript validator Fix error message truncation in TypeScript validator May 4, 2026
Copilot AI requested a review from robgruen May 4, 2026 18:53
@robgruen robgruen marked this pull request as ready for review May 4, 2026 18:55
Comment thread typescript/src/ts/validate.ts
Comment thread typescript/src/ts/validate.ts
- Add test/validate.test.ts with 12 tests using node:test / node:assert:
  * Valid objects passing validation (all three cases)
  * Type mismatch error (backward-compat, code != 2740)
  * Single missing property (TS 2741, backward-compat)
  * 2-5 missing properties (TS 2739, backward-compat)
  * 6+ missing required properties (TS 2740) — verifies full list with
    no "and N more" truncation
  * Optional properties excluded from missing-props list
  * getSchemaText/getTypeName pass-through
  * createModuleTextFromJson output
- Add test/tsconfig.json (compiles to out/, which is gitignored)
- Add "test" script to package.json: tsc -p test && node --test out/validate.test.js

Agent-Logs-Url: https://github.com/microsoft/TypeChat/sessions/b6e951eb-19b1-47a0-bce8-e447ac9a4efe

Co-authored-by: TalZaccai <18443527+TalZaccai@users.noreply.github.com>
Copilot AI requested a review from TalZaccai May 4, 2026 19:15
@robgruen robgruen merged commit 04be84f into main May 4, 2026
13 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Investigate root cause of error message truncation in TypeScript validator

3 participants