Skip to content

Commit 73d964c

Browse files
committed
tweak the widening moment again
1 parent bcbcd65 commit 73d964c

File tree

4 files changed

+96
-3
lines changed

4 files changed

+96
-3
lines changed

src/compiler/checker.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -36225,9 +36225,9 @@ export function createTypeChecker(host: TypeCheckerHost): TypeChecker {
3622536225
if (!getEffectiveTypeAnnotationNode(declaration)) {
3622636226
let type = tryGetTypeAtPosition(context, i);
3622736227
if (type && declaration.initializer) {
36228-
const initializerType = checkDeclarationInitializer(declaration, CheckMode.Normal);
36229-
if (!isTypeAssignableTo(initializerType, type) && isTypeAssignableTo(type, initializerType)) {
36230-
type = widenTypeInferredFromInitializer(declaration, initializerType);
36228+
let initializerType = checkDeclarationInitializer(declaration, CheckMode.Normal);
36229+
if (!isTypeAssignableTo(initializerType, type) && isTypeAssignableTo(type, initializerType = widenTypeInferredFromInitializer(declaration, initializerType))) {
36230+
type = initializerType;
3623136231
}
3623236232
}
3623336233
assignParameterType(parameter, type);
Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
//// [tests/cases/compiler/contextuallyTypedParametersWithInitializers4.ts] ////
2+
3+
=== contextuallyTypedParametersWithInitializers4.ts ===
4+
declare function test<
5+
>test : Symbol(test, Decl(contextuallyTypedParametersWithInitializers4.ts, 0, 0))
6+
7+
TContext,
8+
>TContext : Symbol(TContext, Decl(contextuallyTypedParametersWithInitializers4.ts, 0, 22))
9+
10+
TMethods extends Record<string, (ctx: TContext, ...args: (1 | 2)[]) => unknown>,
11+
>TMethods : Symbol(TMethods, Decl(contextuallyTypedParametersWithInitializers4.ts, 1, 11))
12+
>Record : Symbol(Record, Decl(lib.es5.d.ts, --, --))
13+
>ctx : Symbol(ctx, Decl(contextuallyTypedParametersWithInitializers4.ts, 2, 35))
14+
>TContext : Symbol(TContext, Decl(contextuallyTypedParametersWithInitializers4.ts, 0, 22))
15+
>args : Symbol(args, Decl(contextuallyTypedParametersWithInitializers4.ts, 2, 49))
16+
17+
>(context: TContext, methods: TMethods): void;
18+
>context : Symbol(context, Decl(contextuallyTypedParametersWithInitializers4.ts, 3, 2))
19+
>TContext : Symbol(TContext, Decl(contextuallyTypedParametersWithInitializers4.ts, 0, 22))
20+
>methods : Symbol(methods, Decl(contextuallyTypedParametersWithInitializers4.ts, 3, 20))
21+
>TMethods : Symbol(TMethods, Decl(contextuallyTypedParametersWithInitializers4.ts, 1, 11))
22+
23+
test(
24+
>test : Symbol(test, Decl(contextuallyTypedParametersWithInitializers4.ts, 0, 0))
25+
{
26+
count: 0,
27+
>count : Symbol(count, Decl(contextuallyTypedParametersWithInitializers4.ts, 6, 3))
28+
29+
},
30+
{
31+
checkLimit: (ctx, max = 3) => {},
32+
>checkLimit : Symbol(checkLimit, Decl(contextuallyTypedParametersWithInitializers4.ts, 9, 3))
33+
>ctx : Symbol(ctx, Decl(contextuallyTypedParametersWithInitializers4.ts, 10, 17))
34+
>max : Symbol(max, Decl(contextuallyTypedParametersWithInitializers4.ts, 10, 21))
35+
36+
},
37+
);
38+
Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
//// [tests/cases/compiler/contextuallyTypedParametersWithInitializers4.ts] ////
2+
3+
=== contextuallyTypedParametersWithInitializers4.ts ===
4+
declare function test<
5+
>test : <TContext, TMethods extends Record<string, (ctx: TContext, ...args: (1 | 2)[]) => unknown>>(context: TContext, methods: TMethods) => void
6+
7+
TContext,
8+
TMethods extends Record<string, (ctx: TContext, ...args: (1 | 2)[]) => unknown>,
9+
>ctx : TContext
10+
>args : (1 | 2)[]
11+
12+
>(context: TContext, methods: TMethods): void;
13+
>context : TContext
14+
>methods : TMethods
15+
16+
test(
17+
>test( { count: 0, }, { checkLimit: (ctx, max = 3) => {}, },) : void
18+
>test : <TContext, TMethods extends Record<string, (ctx: TContext, ...args: (1 | 2)[]) => unknown>>(context: TContext, methods: TMethods) => void
19+
{
20+
>{ count: 0, } : { count: number; }
21+
22+
count: 0,
23+
>count : number
24+
>0 : 0
25+
26+
},
27+
{
28+
>{ checkLimit: (ctx, max = 3) => {}, } : { checkLimit: (ctx: { count: number; }, max?: number) => void; }
29+
30+
checkLimit: (ctx, max = 3) => {},
31+
>checkLimit : (ctx: { count: number; }, max?: number) => void
32+
>(ctx, max = 3) => {} : (ctx: { count: number; }, max?: number) => void
33+
>ctx : { count: number; }
34+
>max : number
35+
>3 : 3
36+
37+
},
38+
);
39+
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
// @strict: true
2+
// @noEmit: true
3+
4+
declare function test<
5+
TContext,
6+
TMethods extends Record<string, (ctx: TContext, ...args: (1 | 2)[]) => unknown>,
7+
>(context: TContext, methods: TMethods): void;
8+
9+
test(
10+
{
11+
count: 0,
12+
},
13+
{
14+
checkLimit: (ctx, max = 3) => {},
15+
},
16+
);

0 commit comments

Comments
 (0)