Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 14 additions & 3 deletions tsc/internal/checker/checker.go
Original file line number Diff line number Diff line change
Expand Up @@ -931,6 +931,7 @@ type Checker struct {
_jsxNamespace string
_jsxFactoryEntity *ast.Node
skipDirectInferenceNodes collections.Set[*ast.Node]
skipDirectInferenceTypes collections.Set[*Type]
ctx context.Context
packagesMap map[string]bool
activeMappers []*TypeMapper
Expand Down Expand Up @@ -8304,7 +8305,7 @@ func (c *Checker) checkArrayLiteral(node *ast.Node, checkMode CheckMode) *Type {
return c.createTupleTypeEx(elementTypes, elementInfos, false)
}
if checkMode&CheckModeForceTuple != 0 || inConstContext || inTupleContext {
return c.createArrayLiteralType(c.createTupleTypeEx(elementTypes, elementInfos, inConstContext && !(contextualType != nil && someType(contextualType, c.isMutableArrayLikeType)) /*readonly*/))
return c.createArrayLiteralType(node, c.createTupleTypeEx(elementTypes, elementInfos, inConstContext && !(contextualType != nil && someType(contextualType, c.isMutableArrayLikeType)) /*readonly*/))
}
var elementType *Type
if len(elementTypes) != 0 {
Expand All @@ -8317,13 +8318,23 @@ func (c *Checker) checkArrayLiteral(node *ast.Node, checkMode CheckMode) *Type {
} else {
elementType = core.IfElse(c.strictNullChecks, c.implicitNeverType, c.undefinedWideningType)
}
return c.createArrayLiteralType(c.createArrayTypeEx(elementType, inConstContext))
return c.createArrayLiteralType(node, c.createArrayTypeEx(elementType, inConstContext))
}

func (c *Checker) createArrayLiteralType(t *Type) *Type {
func (c *Checker) createArrayLiteralType(node *ast.Node, t *Type) *Type {
if t.objectFlags&ObjectFlagsReference == 0 {
return t
}
if c.skipDirectInferenceNodes.Has(node) {
// Inference from this array literal is blocked (see runWithInferenceBlockedFromSourceNode). Unlike
// object literal types, array literal types have no symbol referring back to their node and are
// shared between array literals with identical element types, so we create a distinct type here
// that isFromInferenceBlockedSource can recognize.
literalType := c.cloneTypeReference(t)
literalType.objectFlags |= ObjectFlagsArrayLiteral | ObjectFlagsContainsObjectOrArrayLiteral
c.skipDirectInferenceTypes.Add(literalType)
return literalType
}
key := CachedTypeKey{kind: CachedTypeKindArrayLiteralType, typeId: t.id}
if cached, ok := c.cachedTypes[key]; ok {
return cached
Expand Down
2 changes: 1 addition & 1 deletion tsc/internal/checker/inference.go
Original file line number Diff line number Diff line change
Expand Up @@ -1633,7 +1633,7 @@ func (c *Checker) literalTypesWithSameBaseType(types []*Type) bool {
}

func (c *Checker) isFromInferenceBlockedSource(t *Type) bool {
return t.symbol != nil && core.Some(t.symbol.Declarations(), c.isSkipDirectInferenceNode)
return (t.symbol != nil && core.Some(t.symbol.Declarations(), c.isSkipDirectInferenceNode)) || c.skipDirectInferenceTypes.Has(t)
}

func (c *Checker) isSkipDirectInferenceNode(node *ast.Node) bool {
Expand Down
1 change: 1 addition & 0 deletions tsc/internal/checker/services.go
Original file line number Diff line number Diff line change
Expand Up @@ -347,6 +347,7 @@ func (c *Checker) runWithInferenceBlockedFromSourceNode[T any](node *ast.Node, f
c.isInferencePartiallyBlocked = false

c.skipDirectInferenceNodes.Clear()
c.skipDirectInferenceTypes.Clear()
return result
}

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,155 @@
package fourslash_test

import (
"testing"

"github.com/microsoft/TypeScript/tsc/internal/fourslash"
. "github.com/microsoft/TypeScript/tsc/internal/fourslash/tests/util"
"github.com/microsoft/TypeScript/tsc/internal/ls"
"github.com/microsoft/TypeScript/tsc/internal/lsp/lsproto"
"github.com/microsoft/TypeScript/tsc/internal/testutil"
)

func TestCompletionsObjectLiteralInGenericTupleConstraint(t *testing.T) {
t.Parallel()
defer testutil.RecoverAndFail(t, "Panic on fourslash test")
const content = `// @strict: true
type AllPartialObject = {
component?: string
}

declare function extendTheme1<Extensions extends [AllPartialObject]>(
extensions: Extensions
): void

extendTheme1([
{
/*1*/
},
])

declare function extendTheme2<
Extensions extends [AllPartialObject & Record<string, unknown>],
>(extensions: Extensions): void

extendTheme2([
{
/*2*/
},
])

type PartialWithRequiredObject = {
component?: string
requiredField: string
}

declare function extendTheme3<
Extensions extends [PartialWithRequiredObject & Record<string, unknown>],
>(extensions: Extensions): void

extendTheme3([
{
/*3*/
},
])

extendTheme3([
{
requiredField: 'foo',
/*4*/
},
])

declare function extendTheme4<Extensions extends [PartialWithRequiredObject]>(
extensions: Extensions
): Extensions

extendTheme4([
{
requiredField: 'foo',
/*5*/
},
])

declare function extendTheme5<
Extensions extends AllPartialObject & Record<string, unknown>,
>(extensions: [Extensions]): Extensions

extendTheme5([
{
/*6*/
},
])

declare function extendTheme6<Extensions extends PartialWithRequiredObject[]>(
extensions: Extensions
): Extensions

extendTheme6([
{
requiredField: 'foo',
/*7*/
},
])

declare function extendTheme7<Extensions extends AllPartialObject[]>(
options: { extensions: Extensions }
): Extensions

extendTheme7({
extensions: [
{
/*8*/
},
],
})`
f, done := fourslash.NewFourslash(t, nil /*capabilities*/, content)
defer done()

component := &lsproto.CompletionItem{
Label: "component?",
InsertText: new("component"),
FilterText: new("component"),
SortText: new(string(ls.SortTextOptionalMember)),
}
requiredField := &lsproto.CompletionItem{
Label: "requiredField",
SortText: new(string(ls.SortTextLocationPriority)),
}
// Contextual types that include `Record<string, unknown>` make the position a new identifier location,
// which has no commit characters.
noCommitCharacters := []string{}

// Without a string index signature in the constraint.
f.VerifyCompletions(t, []string{"1", "5", "7", "8"}, &fourslash.CompletionsExpectedList{
IsIncomplete: false,
ItemDefaults: &fourslash.CompletionsExpectedItemDefaults{
CommitCharacters: &DefaultCommitCharacters,
EditRange: Ignored,
},
Items: &fourslash.CompletionsExpectedItems{
Exact: []fourslash.CompletionsExpectedItem{component},
},
})
// With a string index signature in the constraint.
f.VerifyCompletions(t, []string{"2", "4", "6"}, &fourslash.CompletionsExpectedList{
IsIncomplete: false,
ItemDefaults: &fourslash.CompletionsExpectedItemDefaults{
CommitCharacters: &noCommitCharacters,
EditRange: Ignored,
},
Items: &fourslash.CompletionsExpectedItems{
Exact: []fourslash.CompletionsExpectedItem{component},
},
})
f.VerifyCompletions(t, "3", &fourslash.CompletionsExpectedList{
IsIncomplete: false,
ItemDefaults: &fourslash.CompletionsExpectedItemDefaults{
CommitCharacters: &noCommitCharacters,
EditRange: Ignored,
},
Items: &fourslash.CompletionsExpectedItems{
Exact: []fourslash.CompletionsExpectedItem{requiredField, component},
},
})
}