@@ -7466,12 +7466,14 @@ func (c *Checker) checkSuperExpression(node *ast.Node) *Type {
7466
7466
isCallExpression := ast.IsCallExpression(node.Parent) && node.Parent.Expression() == node
7467
7467
immediateContainer := getSuperContainer(node, true /*stopOnFunctions*/)
7468
7468
container := immediateContainer
7469
+
7469
7470
// adjust the container reference in case if super is used inside arrow functions with arbitrarily deep nesting
7470
7471
if !isCallExpression {
7471
7472
for container != nil && ast.IsArrowFunction(container) {
7472
7473
container = getSuperContainer(container, true /*stopOnFunctions*/)
7473
7474
}
7474
7475
}
7476
+
7475
7477
isLegalUsageOfSuperExpression := func() bool {
7476
7478
if isCallExpression {
7477
7479
// TS 1.0 SPEC (April 2014): 4.8.1
@@ -7492,6 +7494,7 @@ func (c *Checker) checkSuperExpression(node *ast.Node) *Type {
7492
7494
}
7493
7495
return false
7494
7496
}
7497
+
7495
7498
if container == nil || !isLegalUsageOfSuperExpression() {
7496
7499
// issue more specific error if super is used in computed property name
7497
7500
// class A { foo() { return "1" }}
@@ -8740,7 +8743,7 @@ func (c *Checker) chooseOverload(s *CallState, relation *Relation) *Signature {
8740
8743
if inferenceContext != nil {
8741
8744
inferredTypeParameters = inferenceContext.inferredTypeParameters
8742
8745
}
8743
- checkCandidate = c.getSignatureInstantiation(candidate, typeArgumentTypes, inferredTypeParameters)
8746
+ checkCandidate = c.getSignatureInstantiation(candidate, typeArgumentTypes, ast.IsInJSFile(candidate.declaration), inferredTypeParameters)
8744
8747
// If the original signature has a generic rest type, instantiation may produce a
8745
8748
// signature with different arity and we need to perform another arity check.
8746
8749
if c.getNonArrayRestType(candidate) != nil && !c.hasCorrectArity(s.node, s.args, checkCandidate, s.signatureHelpTrailingComma) {
@@ -8762,7 +8765,7 @@ func (c *Checker) chooseOverload(s *CallState, relation *Relation) *Signature {
8762
8765
s.argCheckMode = CheckModeNormal
8763
8766
if inferenceContext != nil {
8764
8767
typeArgumentTypes := c.instantiateTypes(c.inferTypeArguments(s.node, candidate, s.args, s.argCheckMode, inferenceContext), inferenceContext.mapper)
8765
- checkCandidate = c.getSignatureInstantiation(candidate, typeArgumentTypes, inferenceContext.inferredTypeParameters)
8768
+ checkCandidate = c.getSignatureInstantiation(candidate, typeArgumentTypes, ast.IsInJSFile(candidate.declaration), inferenceContext.inferredTypeParameters)
8766
8769
// If the original signature has a generic rest type, instantiation may produce a
8767
8770
// signature with different arity and we need to perform another arity check.
8768
8771
if c.getNonArrayRestType(candidate) != nil && !c.hasCorrectArity(s.node, s.args, checkCandidate, s.signatureHelpTrailingComma) {
@@ -8908,8 +8911,9 @@ func (c *Checker) hasCorrectTypeArgumentArity(signature *Signature, typeArgument
8908
8911
}
8909
8912
8910
8913
func (c *Checker) checkTypeArguments(signature *Signature, typeArgumentNodes []*ast.Node, reportErrors bool, headMessage *diagnostics.Message) []*Type {
8914
+ isJavaScript := ast.IsInJSFile(signature.declaration)
8911
8915
typeParameters := signature.typeParameters
8912
- typeArgumentTypes := c.fillMissingTypeArguments(core.Map(typeArgumentNodes, c.getTypeFromTypeNode), typeParameters, c.getMinTypeArgumentCount(typeParameters))
8916
+ typeArgumentTypes := c.fillMissingTypeArguments(core.Map(typeArgumentNodes, c.getTypeFromTypeNode), typeParameters, c.getMinTypeArgumentCount(typeParameters), isJavaScript )
8913
8917
var mapper *TypeMapper
8914
8918
for i := range typeArgumentNodes {
8915
8919
// Debug.assert(typeParameters[i] != nil, "Should not call checkTypeArguments with too many type arguments")
@@ -10164,7 +10168,7 @@ func (c *Checker) getInstantiationExpressionType(exprType *Type, node *ast.Node)
10164
10168
return core.SameMap(applicableSignatures, func(sig *Signature) *Signature {
10165
10169
typeArgumentTypes := c.checkTypeArguments(sig, typeArguments.Nodes, true /*reportErrors*/, nil)
10166
10170
if typeArgumentTypes != nil {
10167
- return c.getSignatureInstantiation(sig, typeArgumentTypes, nil)
10171
+ return c.getSignatureInstantiation(sig, typeArgumentTypes, ast.IsInJSFile(sig.declaration), nil)
10168
10172
}
10169
10173
return sig
10170
10174
})
@@ -18346,21 +18350,22 @@ func (c *Checker) getInstantiatedConstructorsForTypeArguments(t *Type, typeArgum
18346
18350
typeArguments := core.Map(typeArgumentNodes, c.getTypeFromTypeNode)
18347
18351
return core.SameMap(signatures, func(sig *Signature) *Signature {
18348
18352
if len(sig.typeParameters) != 0 {
18349
- return c.getSignatureInstantiation(sig, typeArguments, nil)
18353
+ return c.getSignatureInstantiation(sig, typeArguments, ast.IsInJSFile(location), nil)
18350
18354
}
18351
18355
return sig
18352
18356
})
18353
18357
}
18354
18358
18355
18359
func (c *Checker) getConstructorsForTypeArguments(t *Type, typeArgumentNodes []*ast.Node, location *ast.Node) []*Signature {
18356
18360
typeArgCount := len(typeArgumentNodes)
18361
+ isJavaScript := ast.IsInJSFile(location)
18357
18362
return core.Filter(c.getSignaturesOfType(t, SignatureKindConstruct), func(sig *Signature) bool {
18358
- return typeArgCount >= c.getMinTypeArgumentCount(sig.typeParameters) && typeArgCount <= len(sig.typeParameters)
18363
+ return isJavaScript || typeArgCount >= c.getMinTypeArgumentCount(sig.typeParameters) && typeArgCount <= len(sig.typeParameters)
18359
18364
})
18360
18365
}
18361
18366
18362
- func (c *Checker) getSignatureInstantiation(sig *Signature, typeArguments []*Type, inferredTypeParameters []*Type) *Signature {
18363
- instantiatedSignature := c.getSignatureInstantiationWithoutFillingInTypeArguments(sig, c.fillMissingTypeArguments(typeArguments, sig.typeParameters, c.getMinTypeArgumentCount(sig.typeParameters)))
18367
+ func (c *Checker) getSignatureInstantiation(sig *Signature, typeArguments []*Type, isJavaScript bool, inferredTypeParameters []*Type) *Signature {
18368
+ instantiatedSignature := c.getSignatureInstantiationWithoutFillingInTypeArguments(sig, c.fillMissingTypeArguments(typeArguments, sig.typeParameters, c.getMinTypeArgumentCount(sig.typeParameters), isJavaScript ))
18364
18369
if len(inferredTypeParameters) != 0 {
18365
18370
returnSignature := c.getSingleCallOrConstructSignature(c.getReturnTypeOfSignature(instantiatedSignature))
18366
18371
if returnSignature != nil {
@@ -18498,12 +18503,13 @@ func (c *Checker) createCanonicalSignature(signature *Signature) *Signature {
18498
18503
// where different generations of the same type parameter are in scope). This leads to a lot of new type
18499
18504
// identities, and potentially a lot of work comparing those identities, so here we create an instantiation
18500
18505
// that uses the original type identities for all unconstrained type parameters.
18501
- return c.getSignatureInstantiation(signature, core.Map(signature.typeParameters, func(tp *Type) *Type {
18506
+ typeArguments := core.Map(signature.typeParameters, func(tp *Type) *Type {
18502
18507
if tp.Target() != nil && c.getConstraintOfTypeParameter(tp.Target()) == nil {
18503
18508
return tp.Target()
18504
18509
}
18505
18510
return tp
18506
- }), nil)
18511
+ })
18512
+ return c.getSignatureInstantiation(signature, typeArguments, ast.IsInJSFile(signature.declaration), nil /*inferredTypeParameters*/)
18507
18513
}
18508
18514
18509
18515
func (c *Checker) getBaseSignature(signature *Signature) *Signature {
@@ -18563,7 +18569,7 @@ func (c *Checker) instantiateSignatureInContextOf(signature *Signature, contextu
18563
18569
c.inferTypes(context.inferences, source, target, InferencePriorityReturnType, false)
18564
18570
})
18565
18571
}
18566
- return c.getSignatureInstantiation(signature, c.getInferredTypes(context), nil)
18572
+ return c.getSignatureInstantiation(signature, c.getInferredTypes(context), ast.IsInJSFile(contextualSignature.declaration), nil /*inferredTypeParameters*/ )
18567
18573
}
18568
18574
18569
18575
func (c *Checker) resolveBaseTypesOfInterface(t *Type) {
@@ -19884,16 +19890,17 @@ func (c *Checker) getDefaultConstructSignatures(classType *Type) []*Signature {
19884
19890
return []*Signature{c.newSignature(flags, nil, classType.AsInterfaceType().LocalTypeParameters(), nil, nil, classType, nil, 0)}
19885
19891
}
19886
19892
baseTypeNode := getBaseTypeNodeOfClass(classType)
19893
+ isJavaScript := declaration != nil && ast.IsInJSFile(declaration)
19887
19894
typeArguments := c.getTypeArgumentsFromNode(baseTypeNode)
19888
19895
typeArgCount := len(typeArguments)
19889
19896
var result []*Signature
19890
19897
for _, baseSig := range baseSignatures {
19891
19898
minTypeArgumentCount := c.getMinTypeArgumentCount(baseSig.typeParameters)
19892
19899
typeParamCount := len(baseSig.typeParameters)
19893
- if typeArgCount >= minTypeArgumentCount && typeArgCount <= typeParamCount {
19900
+ if isJavaScript || typeArgCount >= minTypeArgumentCount && typeArgCount <= typeParamCount {
19894
19901
var sig *Signature
19895
19902
if typeParamCount != 0 {
19896
- sig = c.createSignatureInstantiation(baseSig, c.fillMissingTypeArguments(typeArguments, baseSig.typeParameters, minTypeArgumentCount))
19903
+ sig = c.createSignatureInstantiation(baseSig, c.fillMissingTypeArguments(typeArguments, baseSig.typeParameters, minTypeArgumentCount, isJavaScript ))
19897
19904
} else {
19898
19905
sig = c.cloneSignature(baseSig)
19899
19906
}
@@ -20987,7 +20994,7 @@ func (c *Checker) getTypeArguments(t *Type) []*Type {
20987
20994
}
20988
20995
20989
20996
func (c *Checker) getEffectiveTypeArguments(node *ast.Node, typeParameters []*Type) []*Type {
20990
- return c.fillMissingTypeArguments(core.Map(node.TypeArguments(), c.getTypeFromTypeNode), typeParameters, c.getMinTypeArgumentCount(typeParameters))
20997
+ return c.fillMissingTypeArguments(core.Map(node.TypeArguments(), c.getTypeFromTypeNode), typeParameters, c.getMinTypeArgumentCount(typeParameters), ast.IsInJSFile(node) )
20991
20998
}
20992
20999
20993
21000
// Gets the minimum number of type arguments needed to satisfy all non-optional type parameters.
@@ -21007,32 +21014,45 @@ func (c *Checker) hasTypeParameterDefault(t *Type) bool {
21007
21014
})
21008
21015
}
21009
21016
21010
- func (c *Checker) fillMissingTypeArguments(typeArguments []*Type, typeParameters []*Type, minTypeArgumentCount int) []*Type {
21017
+ func (c *Checker) fillMissingTypeArguments(typeArguments []*Type, typeParameters []*Type, minTypeArgumentCount int, isJavaScriptImplicitAny bool ) []*Type {
21011
21018
numTypeParameters := len(typeParameters)
21012
21019
if numTypeParameters == 0 {
21013
21020
return nil
21014
21021
}
21015
21022
numTypeArguments := len(typeArguments)
21016
- if numTypeArguments >= minTypeArgumentCount && numTypeArguments < numTypeParameters {
21023
+ if isJavaScriptImplicitAny || ( numTypeArguments >= minTypeArgumentCount && numTypeArguments < numTypeParameters) {
21017
21024
result := make([]*Type, numTypeParameters)
21018
21025
copy(result, typeArguments)
21019
21026
// Map invalid forward references in default types to the error type
21020
21027
for i := numTypeArguments; i < numTypeParameters; i++ {
21021
21028
result[i] = c.errorType
21022
21029
}
21030
+ baseDefaultType := c.getDefaultTypeArgumentType(isJavaScriptImplicitAny)
21023
21031
for i := numTypeArguments; i < numTypeParameters; i++ {
21024
21032
defaultType := c.getDefaultFromTypeParameter(typeParameters[i])
21033
+
21034
+ if isJavaScriptImplicitAny && defaultType != nil && (c.isTypeIdenticalTo(defaultType, c.unknownType) || c.isTypeIdenticalTo(defaultType, c.emptyObjectType)) {
21035
+ defaultType = c.anyType
21036
+ }
21037
+
21025
21038
if defaultType != nil {
21026
21039
result[i] = c.instantiateType(defaultType, newTypeMapper(typeParameters, result))
21027
21040
} else {
21028
- result[i] = c.unknownType
21041
+ result[i] = baseDefaultType
21029
21042
}
21030
21043
}
21031
21044
return result
21032
21045
}
21033
21046
return typeArguments
21034
21047
}
21035
21048
21049
+ func (c *Checker) getDefaultTypeArgumentType(isInJavaScriptFile bool) *Type {
21050
+ if isInJavaScriptFile {
21051
+ return c.anyType
21052
+ }
21053
+ return c.unknownType
21054
+ }
21055
+
21036
21056
// Gets the default type for a type parameter. If the type parameter is the result of an instantiation,
21037
21057
// this gets the instantiated default type of its target. If the type parameter has no default type or
21038
21058
// the default is circular, `undefined` is returned.
@@ -22072,6 +22092,8 @@ func (c *Checker) getTypeReferenceType(node *ast.Node, symbol *ast.Symbol) *Type
22072
22092
if res != nil && c.checkNoTypeArguments(node, symbol) {
22073
22093
return c.getRegularTypeOfLiteralType(res)
22074
22094
}
22095
+
22096
+ // !!! Resolving values as types for JS
22075
22097
return c.errorType
22076
22098
}
22077
22099
@@ -22085,23 +22107,37 @@ func (c *Checker) getTypeFromClassOrInterfaceReference(node *ast.Node, symbol *a
22085
22107
if len(typeParameters) != 0 {
22086
22108
numTypeArguments := len(node.TypeArguments())
22087
22109
minTypeArgumentCount := c.getMinTypeArgumentCount(typeParameters)
22088
- if numTypeArguments < minTypeArgumentCount || numTypeArguments > len(typeParameters) {
22089
- message := diagnostics.Generic_type_0_requires_1_type_argument_s
22090
- if minTypeArgumentCount < len(typeParameters) {
22091
- message = diagnostics.Generic_type_0_requires_between_1_and_2_type_arguments
22110
+ isJs := ast.IsInJSFile(node)
22111
+ isJsImplicitAny := !c.noImplicitAny && isJs
22112
+ if !isJsImplicitAny && (numTypeArguments < minTypeArgumentCount || numTypeArguments > len(typeParameters)) {
22113
+ var message *diagnostics.Message
22114
+
22115
+ missingAugmentsTag := isJs && ast.IsExpressionWithTypeArguments(node) && !ast.IsJSDocAugmentsTag(node.Parent)
22116
+ if missingAugmentsTag {
22117
+ message = diagnostics.Expected_0_type_arguments_provide_these_with_an_extends_tag
22118
+ if minTypeArgumentCount < len(typeParameters) {
22119
+ message = diagnostics.Expected_0_1_type_arguments_provide_these_with_an_extends_tag
22120
+ }
22121
+ } else {
22122
+ message = diagnostics.Generic_type_0_requires_1_type_argument_s
22123
+ if minTypeArgumentCount < len(typeParameters) {
22124
+ message = diagnostics.Generic_type_0_requires_between_1_and_2_type_arguments
22125
+ }
22092
22126
}
22093
- typeStr := c.TypeToString(t) // !!! /*enclosingDeclaration*/, nil, TypeFormatFlagsWriteArrayAsGenericType
22127
+ typeStr := c.TypeToStringEx(t, nil /*enclosingDeclaration*/, TypeFormatFlagsWriteArrayAsGenericType)
22094
22128
c.error(node, message, typeStr, minTypeArgumentCount, len(typeParameters))
22095
- // TODO: Adopt same permissive behavior in TS as in JS to reduce follow-on editing experience failures (requires editing fillMissingTypeArguments)
22096
- return c.errorType
22129
+ if !isJs {
22130
+ // TODO: Adopt same permissive behavior in TS as in JS to reduce follow-on editing experience failures (requires editing fillMissingTypeArguments)
22131
+ return c.errorType
22132
+ }
22097
22133
}
22098
22134
if node.Kind == ast.KindTypeReference && c.isDeferredTypeReferenceNode(node, numTypeArguments != len(typeParameters)) {
22099
22135
return c.createDeferredTypeReference(t, node, nil /*mapper*/, nil /*alias*/)
22100
22136
}
22101
22137
// In a type reference, the outer type parameters of the referenced class or interface are automatically
22102
22138
// supplied as type arguments and the type reference only specifies arguments for the local type parameters
22103
22139
// of the class or interface.
22104
- localTypeArguments := c.fillMissingTypeArguments(c.getTypeArgumentsFromNode(node), typeParameters, minTypeArgumentCount)
22140
+ localTypeArguments := c.fillMissingTypeArguments(c.getTypeArgumentsFromNode(node), typeParameters, minTypeArgumentCount, isJs )
22105
22141
typeArguments := append(d.OuterTypeParameters(), localTypeArguments...)
22106
22142
return c.createTypeReference(t, typeArguments)
22107
22143
}
@@ -22542,7 +22578,7 @@ func (c *Checker) getTypeAliasInstantiation(symbol *ast.Symbol, typeArguments []
22542
22578
key := getTypeAliasInstantiationKey(typeArguments, alias)
22543
22579
instantiation := links.instantiations[key]
22544
22580
if instantiation == nil {
22545
- mapper := newTypeMapper(typeParameters, c.fillMissingTypeArguments(typeArguments, typeParameters, c.getMinTypeArgumentCount(typeParameters)))
22581
+ mapper := newTypeMapper(typeParameters, c.fillMissingTypeArguments(typeArguments, typeParameters, c.getMinTypeArgumentCount(typeParameters), ast.IsInJSFile(symbol.ValueDeclaration) ))
22546
22582
instantiation = c.instantiateTypeWithAlias(t, mapper, alias)
22547
22583
links.instantiations[key] = instantiation
22548
22584
}
0 commit comments