Skip to content
Closed
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
14 changes: 10 additions & 4 deletions src/compiler/checker.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11601,11 +11601,17 @@ export function createTypeChecker(host: TypeCheckerHost): TypeChecker {
// Return the type of a binding element parent. We check SymbolLinks first to see if a type has been
// assigned by contextual typing.
function getTypeForBindingElementParent(node: BindingElementGrandparent, checkMode: CheckMode) {
if (checkMode !== CheckMode.Normal) {
return getTypeForVariableLikeDeclaration(node, /*includeOptionality*/ false, checkMode);
if (checkMode === CheckMode.Normal) {
// We can use a cached resolved type if no optionality was included in that type.
const symbol = getSymbolOfDeclaration(node);
if (symbol) {
const type = getSymbolLinks(symbol).type;
if (type && !(strictNullChecks && isOptionalDeclaration(node))) {
return type;
}
}
}
const symbol = getSymbolOfDeclaration(node);
return symbol && getSymbolLinks(symbol).type || getTypeForVariableLikeDeclaration(node, /*includeOptionality*/ false, checkMode);
return getTypeForVariableLikeDeclaration(node, /*includeOptionality*/ false, checkMode);
}

function getRestType(source: Type, properties: PropertyName[], symbol: Symbol | undefined): Type {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
//// [tests/cases/compiler/bindingPatternOptionalParameterCached.ts] ////

=== bindingPatternOptionalParameterCached.ts ===
// https://github.com/microsoft/typescript-go/issues/3276

// There should be no error in this test, but previously there was because of the
// declaration of mock. Commenting it out would make the error disappear.

export const mock: I = {
>mock : Symbol(mock, Decl(bindingPatternOptionalParameterCached.ts, 5, 12))
>I : Symbol(I, Decl(bindingPatternOptionalParameterCached.ts, 7, 2))

m: (_) => {},
>m : Symbol(m, Decl(bindingPatternOptionalParameterCached.ts, 5, 24))
>_ : Symbol(_, Decl(bindingPatternOptionalParameterCached.ts, 6, 8))

};

export interface I {
>I : Symbol(I, Decl(bindingPatternOptionalParameterCached.ts, 7, 2))

m({ x }?: { x: boolean }): void
>m : Symbol(I.m, Decl(bindingPatternOptionalParameterCached.ts, 9, 20))
>x : Symbol(x, Decl(bindingPatternOptionalParameterCached.ts, 10, 7))
>x : Symbol(x, Decl(bindingPatternOptionalParameterCached.ts, 10, 15))
}

Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
//// [tests/cases/compiler/bindingPatternOptionalParameterCached.ts] ////

=== bindingPatternOptionalParameterCached.ts ===
// https://github.com/microsoft/typescript-go/issues/3276

// There should be no error in this test, but previously there was because of the
// declaration of mock. Commenting it out would make the error disappear.

export const mock: I = {
>mock : I
> : ^
>{ m: (_) => {},} : { m: (_: { x: boolean; } | undefined) => void; }
> : ^^^^^^ ^^^^^^^ ^^^^^^^^^^^^^^^^^^^^^^^^^^^

m: (_) => {},
>m : (_: { x: boolean; } | undefined) => void
> : ^ ^^^^^^^ ^^^^^^^^^^^^^^^^^^^^^^^^
>(_) => {} : (_: { x: boolean; } | undefined) => void
> : ^ ^^^^^^^ ^^^^^^^^^^^^^^^^^^^^^^^^
>_ : { x: boolean; } | undefined
> : ^^^^^ ^^^^^^^^^^^^^^^

};

export interface I {
m({ x }?: { x: boolean }): void
>m : ({ x }?: { x: boolean; }) => void
> : ^ ^^^ ^^^^^
>x : boolean
> : ^^^^^^^
>x : boolean
> : ^^^^^^^
}

15 changes: 15 additions & 0 deletions tests/cases/compiler/bindingPatternOptionalParameterCached.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
// @strict: true
// @noEmit: true

// https://github.com/microsoft/typescript-go/issues/3276

// There should be no error in this test, but previously there was because of the
// declaration of mock. Commenting it out would make the error disappear.

export const mock: I = {
m: (_) => {},
};

export interface I {
m({ x }?: { x: boolean }): void
}
Loading