Skip to content

Commit b7af3a2

Browse files
committed
Port tsgo PR 3276 for testing
1 parent 7881fe5 commit b7af3a2

4 files changed

Lines changed: 86 additions & 4 deletions

File tree

src/compiler/checker.ts

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -11601,11 +11601,17 @@ export function createTypeChecker(host: TypeCheckerHost): TypeChecker {
1160111601
// Return the type of a binding element parent. We check SymbolLinks first to see if a type has been
1160211602
// assigned by contextual typing.
1160311603
function getTypeForBindingElementParent(node: BindingElementGrandparent, checkMode: CheckMode) {
11604-
if (checkMode !== CheckMode.Normal) {
11605-
return getTypeForVariableLikeDeclaration(node, /*includeOptionality*/ false, checkMode);
11604+
if (checkMode === CheckMode.Normal) {
11605+
// We can use a cached resolved type if no optionality was included in that type.
11606+
const symbol = getSymbolOfDeclaration(node);
11607+
if (symbol) {
11608+
const type = getSymbolLinks(symbol).type;
11609+
if (type && !(strictNullChecks && isOptionalDeclaration(node))) {
11610+
return type;
11611+
}
11612+
}
1160611613
}
11607-
const symbol = getSymbolOfDeclaration(node);
11608-
return symbol && getSymbolLinks(symbol).type || getTypeForVariableLikeDeclaration(node, /*includeOptionality*/ false, checkMode);
11614+
return getTypeForVariableLikeDeclaration(node, /*includeOptionality*/ false, checkMode);
1160911615
}
1161011616

1161111617
function getRestType(source: Type, properties: PropertyName[], symbol: Symbol | undefined): Type {
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
//// [tests/cases/compiler/bindingPatternOptionalParameterCached.ts] ////
2+
3+
=== bindingPatternOptionalParameterCached.ts ===
4+
// https://github.com/microsoft/typescript-go/issues/3276
5+
6+
// There should be no error in this test, but previously there was because of the
7+
// declaration of mock. Commenting it out would make the error disappear.
8+
9+
export const mock: I = {
10+
>mock : Symbol(mock, Decl(bindingPatternOptionalParameterCached.ts, 5, 12))
11+
>I : Symbol(I, Decl(bindingPatternOptionalParameterCached.ts, 7, 2))
12+
13+
m: (_) => {},
14+
>m : Symbol(m, Decl(bindingPatternOptionalParameterCached.ts, 5, 24))
15+
>_ : Symbol(_, Decl(bindingPatternOptionalParameterCached.ts, 6, 8))
16+
17+
};
18+
19+
export interface I {
20+
>I : Symbol(I, Decl(bindingPatternOptionalParameterCached.ts, 7, 2))
21+
22+
m({ x }?: { x: boolean }): void
23+
>m : Symbol(I.m, Decl(bindingPatternOptionalParameterCached.ts, 9, 20))
24+
>x : Symbol(x, Decl(bindingPatternOptionalParameterCached.ts, 10, 7))
25+
>x : Symbol(x, Decl(bindingPatternOptionalParameterCached.ts, 10, 15))
26+
}
27+
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
//// [tests/cases/compiler/bindingPatternOptionalParameterCached.ts] ////
2+
3+
=== bindingPatternOptionalParameterCached.ts ===
4+
// https://github.com/microsoft/typescript-go/issues/3276
5+
6+
// There should be no error in this test, but previously there was because of the
7+
// declaration of mock. Commenting it out would make the error disappear.
8+
9+
export const mock: I = {
10+
>mock : I
11+
> : ^
12+
>{ m: (_) => {},} : { m: (_: { x: boolean; } | undefined) => void; }
13+
> : ^^^^^^ ^^^^^^^ ^^^^^^^^^^^^^^^^^^^^^^^^^^^
14+
15+
m: (_) => {},
16+
>m : (_: { x: boolean; } | undefined) => void
17+
> : ^ ^^^^^^^ ^^^^^^^^^^^^^^^^^^^^^^^^
18+
>(_) => {} : (_: { x: boolean; } | undefined) => void
19+
> : ^ ^^^^^^^ ^^^^^^^^^^^^^^^^^^^^^^^^
20+
>_ : { x: boolean; } | undefined
21+
> : ^^^^^ ^^^^^^^^^^^^^^^
22+
23+
};
24+
25+
export interface I {
26+
m({ x }?: { x: boolean }): void
27+
>m : ({ x }?: { x: boolean; }) => void
28+
> : ^ ^^^ ^^^^^
29+
>x : boolean
30+
> : ^^^^^^^
31+
>x : boolean
32+
> : ^^^^^^^
33+
}
34+
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
// @strict: true
2+
// @noEmit: true
3+
4+
// https://github.com/microsoft/typescript-go/issues/3276
5+
6+
// There should be no error in this test, but previously there was because of the
7+
// declaration of mock. Commenting it out would make the error disappear.
8+
9+
export const mock: I = {
10+
m: (_) => {},
11+
};
12+
13+
export interface I {
14+
m({ x }?: { x: boolean }): void
15+
}

0 commit comments

Comments
 (0)