* For TypeScript error 2740 (missing required properties, truncated with "and N more"), * uses the type checker to compute the full list of missing required properties from the * variable declaration at `position` in `file`. Returns `undefined` if the declaration * cannot be located o
(checker: ts.TypeChecker, file: ts.SourceFile, position: number)
| 80 | * cannot be located or yields no missing properties (fallback to the original message). |
| 81 | */ |
| 82 | function expandMissingPropertiesMessage(checker: ts.TypeChecker, file: ts.SourceFile, position: number): string | undefined { |
| 83 | for (const stmt of file.statements) { |
| 84 | if (ts.isVariableStatement(stmt)) { |
| 85 | for (const decl of stmt.declarationList.declarations) { |
| 86 | // Match the specific declaration that spans the diagnostic position. |
| 87 | // Use getStart() to exclude leading trivia from the range check. |
| 88 | if (decl.getStart(file) <= position && position <= decl.end && |
| 89 | decl.type && ts.isTypeReferenceNode(decl.type) && decl.initializer) { |
| 90 | const targetType = checker.getTypeAtLocation(decl.type); |
| 91 | const sourceType = checker.getTypeAtLocation(decl.initializer); |
| 92 | const sourceProps = new Set(sourceType.getProperties().map(p => p.name)); |
| 93 | const missingProps = targetType.getProperties() |
| 94 | .filter(p => !(p.flags & ts.SymbolFlags.Optional) && !sourceProps.has(p.name)) |
| 95 | .map(p => p.name); |
| 96 | if (missingProps.length > 0) { |
| 97 | const srcStr = checker.typeToString(sourceType, undefined, ts.TypeFormatFlags.NoTruncation); |
| 98 | const tgtStr = checker.typeToString(targetType, undefined, ts.TypeFormatFlags.NoTruncation); |
| 99 | return `Type '${srcStr}' is missing the following properties from type '${tgtStr}': ${missingProps.join(", ")}`; |
| 100 | } |
| 101 | } |
| 102 | } |
| 103 | } |
| 104 | } |
| 105 | return undefined; |
| 106 | } |
| 107 | |
| 108 | function createModuleTextFromJson(jsonObject: object) { |
| 109 | return success(`import { ${typeName} } from './schema';\nconst json: ${typeName} = ${JSON.stringify(jsonObject, undefined, 2)};\n`); |
no outgoing calls
no test coverage detected
searching dependent graphs…