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

Method normalize

internal/checker/checker.go:23272–23336  ·  view source on GitHub ↗
(c *Checker, elementTypes []*Type, elementInfos []TupleElementInfo)

Source from the content-addressed store, hash-verified

23270}
23271
23272func (n *TupleNormalizer) normalize(c *Checker, elementTypes []*Type, elementInfos []TupleElementInfo) bool {
23273 n.c = c
23274 n.lastRequiredIndex = -1
23275 n.firstRestIndex = -1
23276 n.lastOptionalOrRestIndex = -1
23277 for i, t := range elementTypes {
23278 info := elementInfos[i]
23279 if info.flags&ElementFlagsVariadic != 0 {
23280 if t.flags&TypeFlagsAny != 0 {
23281 n.add(t, TupleElementInfo{flags: ElementFlagsRest, labeledDeclaration: info.labeledDeclaration})
23282 } else if t.flags&TypeFlagsInstantiableNonPrimitive != 0 || c.isGenericMappedType(t) {
23283 // Generic variadic elements stay as they are.
23284 n.add(t, info)
23285 } else if isTupleType(t) {
23286 spreadTypes := c.getElementTypes(t)
23287 if len(spreadTypes)+len(n.types) >= 10_000 {
23288 message := core.IfElse(ast.IsPartOfTypeNode(c.currentNode),
23289 diagnostics.Type_produces_a_tuple_type_that_is_too_large_to_represent,
23290 diagnostics.Expression_produces_a_tuple_type_that_is_too_large_to_represent)
23291 c.error(c.currentNode, message)
23292 return false
23293 }
23294 // Spread variadic elements with tuple types into the resulting tuple.
23295 spreadInfos := t.TargetTupleType().elementInfos
23296 for j, s := range spreadTypes {
23297 n.add(s, spreadInfos[j])
23298 }
23299 } else {
23300 // Treat everything else as an array type and create a rest element.
23301 var s *Type
23302 if c.isArrayLikeType(t) {
23303 s = c.getIndexTypeOfType(t, c.numberType)
23304 }
23305 if s == nil {
23306 s = c.errorType
23307 }
23308 n.add(s, TupleElementInfo{flags: ElementFlagsRest, labeledDeclaration: info.labeledDeclaration})
23309 }
23310 } else {
23311 // Copy other element kinds with no change.
23312 n.add(t, info)
23313 }
23314 }
23315 // Turn optional elements preceding the last required element into required elements
23316 for i := range n.lastRequiredIndex {
23317 if n.infos[i].flags&ElementFlagsOptional != 0 {
23318 n.infos[i].flags = ElementFlagsRequired
23319 }
23320 }
23321 if n.firstRestIndex >= 0 && n.firstRestIndex < n.lastOptionalOrRestIndex {
23322 // Turn elements between first rest and last optional/rest into a single rest element
23323 var types []*Type
23324 for i := n.firstRestIndex; i <= n.lastOptionalOrRestIndex; i++ {
23325 t := n.types[i]
23326 if n.infos[i].flags&ElementFlagsVariadic != 0 {
23327 t = c.getIndexedAccessType(t, c.numberType)
23328 }
23329 types = append(types, t)

Callers 1

Calls 15

addMethod · 0.95
IfElseFunction · 0.92
IsPartOfTypeNodeFunction · 0.92
lenFunction · 0.85
isGenericMappedTypeMethod · 0.80
getElementTypesMethod · 0.80
TargetTupleTypeMethod · 0.80
getIndexTypeOfTypeMethod · 0.80
getIndexedAccessTypeMethod · 0.80
getUnionTypeMethod · 0.80
isTupleTypeFunction · 0.70
errorMethod · 0.65

Tested by

no test coverage detected