MCPcopy Create free account
hub / github.com/microsoft/typescript-go / getBaseConstructorTypeOfClass

Method getBaseConstructorTypeOfClass

internal/checker/checker.go:16870–16920  ·  view source on GitHub ↗

** * The base constructor of a class can resolve to * * undefinedType if the class has no extends clause, * * errorType if an error occurred during resolution of the extends expression, * * nullType if the extends expression is the null value, * * anyType if the extends expression has type any,

(t *Type)

Source from the content-addressed store, hash-verified

16868 * * an object type with at least one construct signature.
16869 */
16870func (c *Checker) getBaseConstructorTypeOfClass(t *Type) *Type {
16871 data := t.AsInterfaceType()
16872 if data.resolvedBaseConstructorType != nil {
16873 return data.resolvedBaseConstructorType
16874 }
16875 baseTypeNode := getBaseTypeNodeOfClass(t)
16876 if baseTypeNode == nil {
16877 data.resolvedBaseConstructorType = c.undefinedType
16878 return data.resolvedBaseConstructorType
16879 }
16880 if !c.pushTypeResolution(t, TypeSystemPropertyNameResolvedBaseConstructorType) {
16881 return c.errorType
16882 }
16883 baseConstructorType := c.checkExpression(baseTypeNode.Expression())
16884 if baseConstructorType.flags&(TypeFlagsObject|TypeFlagsIntersection) != 0 {
16885 // Resolving the members of a class requires us to resolve the base class of that class.
16886 // We force resolution here such that we catch circularities now.
16887 c.resolveStructuredTypeMembers(baseConstructorType)
16888 }
16889 if !c.popTypeResolution() {
16890 c.error(t.symbol.ValueDeclaration, diagnostics.X_0_is_referenced_directly_or_indirectly_in_its_own_base_expression, c.symbolToString(t.symbol))
16891 if data.resolvedBaseConstructorType == nil {
16892 data.resolvedBaseConstructorType = c.errorType
16893 }
16894 return data.resolvedBaseConstructorType
16895 }
16896 if baseConstructorType.flags&TypeFlagsAny == 0 && baseConstructorType != c.nullWideningType && !c.isConstructorType(baseConstructorType) {
16897 err := c.error(baseTypeNode.Expression(), diagnostics.Type_0_is_not_a_constructor_function_type, c.TypeToString(baseConstructorType))
16898 if baseConstructorType.flags&TypeFlagsTypeParameter != 0 {
16899 constraint := c.getConstraintFromTypeParameter(baseConstructorType)
16900 var ctorReturn *Type = c.unknownType
16901 if constraint != nil {
16902 ctorSigs := c.getSignaturesOfType(constraint, SignatureKindConstruct)
16903 if len(ctorSigs) != 0 {
16904 ctorReturn = c.getReturnTypeOfSignature(ctorSigs[0])
16905 }
16906 }
16907 if baseConstructorType.symbol.Declarations != nil {
16908 err.AddRelatedInfo(createDiagnosticForNode(baseConstructorType.symbol.Declarations[0], diagnostics.Did_you_mean_for_0_to_be_constrained_to_type_new_args_Colon_any_1, c.symbolToString(baseConstructorType.symbol), c.TypeToString(ctorReturn)))
16909 }
16910 }
16911 if data.resolvedBaseConstructorType == nil {
16912 data.resolvedBaseConstructorType = c.errorType
16913 }
16914 return data.resolvedBaseConstructorType
16915 }
16916 if data.resolvedBaseConstructorType == nil {
16917 data.resolvedBaseConstructorType = baseConstructorType
16918 }
16919 return data.resolvedBaseConstructorType
16920}
16921
16922func (c *Checker) isFunctionType(t *Type) bool {
16923 return t.flags&TypeFlagsObject != 0 && len(c.getSignaturesOfType(t, SignatureKindCall)) > 0

Calls 15

pushTypeResolutionMethod · 0.95
checkExpressionMethod · 0.95
popTypeResolutionMethod · 0.95
errorMethod · 0.95
symbolToStringMethod · 0.95
isConstructorTypeMethod · 0.95
TypeToStringMethod · 0.95
getSignaturesOfTypeMethod · 0.95
getBaseTypeNodeOfClassFunction · 0.85

Tested by

no test coverage detected