ts-simple-ast
Version:
TypeScript compiler wrapper for AST navigation and code generation.
74 lines (73 loc) • 2.88 kB
JavaScript
;
var __extends = (this && this.__extends)/* istanbul ignore next */ || (function () {
var extendStatics = Object.setPrototypeOf ||
({ __proto__: [] } instanceof Array && function (d, b) { d.__proto__ = b; }) ||
function (d, b) { for (var p in b) if (b.hasOwnProperty(p)) d[p] = b[p]; };
return function (d, b) {
extendStatics(d, b);
function __() { this.constructor = d; }
d.prototype = b === null ? Object.create(b) : (__.prototype = b.prototype, new __());
};
})();
Object.defineProperty(exports, "__esModule", { value: true });
var errors = require("./../../errors");
var utils_1 = require("./../../utils");
var Type_1 = require("./Type");
var TypeParameter = /** @class */ (function (_super) {
__extends(TypeParameter, _super);
function TypeParameter() {
return _super !== null && _super.apply(this, arguments) || this;
}
/**
* Gets the constraint or throws if it doesn't exist.
*/
TypeParameter.prototype.getConstraintOrThrow = function () {
return errors.throwIfNullOrUndefined(this.getConstraint(), "Expected type parameter to have a constraint.");
};
/**
* Gets the constraint type.
*/
TypeParameter.prototype.getConstraint = function () {
var declaration = this._getTypeParameterDeclaration();
if (declaration == null)
return undefined;
var constraintNode = declaration.getConstraintNode();
if (constraintNode == null)
return undefined;
return this.global.typeChecker.getTypeAtLocation(constraintNode);
};
/**
* Gets the default type or throws if it doesn't exist.
*/
TypeParameter.prototype.getDefaultOrThrow = function () {
return errors.throwIfNullOrUndefined(this.getDefault(), "Expected type parameter to have a default type.");
};
/**
* Gets the default type or undefined if it doesn't exist.
*/
TypeParameter.prototype.getDefault = function () {
var declaration = this._getTypeParameterDeclaration();
if (declaration == null)
return undefined;
var defaultNode = declaration.getDefaultNode();
if (defaultNode == null)
return undefined;
return this.global.typeChecker.getTypeAtLocation(defaultNode);
};
/**
* @internal
*/
TypeParameter.prototype._getTypeParameterDeclaration = function () {
var symbol = this.getSymbol();
if (symbol == null)
return undefined;
var declaration = symbol.getDeclarations()[0];
if (declaration == null)
return undefined;
if (!utils_1.TypeGuards.isTypeParameterDeclaration(declaration))
return undefined;
return declaration;
};
return TypeParameter;
}(Type_1.Type));
exports.TypeParameter = TypeParameter;