tsd
Version:
Check TypeScript type definitions
41 lines (40 loc) • 1.53 kB
JavaScript
Object.defineProperty(exports, "__esModule", { value: true });
exports.isNotAssignable = void 0;
const utils_1 = require("../../utils");
/**
* Asserts that the argument of the assertion is not assignable to the generic type of the assertion.
*
* @param checker - The TypeScript type checker.
* @param nodes - The `expectNotAssignable` AST nodes.
* @return List of custom diagnostics.
*/
const isNotAssignable = (checker, nodes) => {
const diagnostics = [];
if (!nodes) {
return diagnostics;
}
for (const node of nodes) {
if (!node.typeArguments) {
// Skip if the node does not have generics
continue;
}
// Retrieve the type to be expected. This is the type inside the generic.
const expectedType = checker.getTypeFromTypeNode(node.typeArguments[0]);
const receivedType = checker.getTypeAtLocation(node.arguments[0]);
if (checker.isTypeAssignableTo(receivedType, expectedType)) {
/**
* The argument type is assignable to the expected type, we don't want this so add a diagnostic.
*/
diagnostics.push((0, utils_1.makeDiagnosticWithDiff)({
message: 'Argument of type `{receivedType}` is assignable to parameter of type `{expectedType}`.',
expectedType,
receivedType,
checker,
node,
}));
}
}
return diagnostics;
};
exports.isNotAssignable = isNotAssignable;
;