Skip to content

Commit 6aabb88

Browse files
committed
Prefer the initializer type over contextual never parameter type
1 parent c266e47 commit 6aabb88

File tree

4 files changed

+112
-3
lines changed

4 files changed

+112
-3
lines changed

src/compiler/checker.ts

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -36062,9 +36062,13 @@ export function createTypeChecker(host: TypeCheckerHost): TypeChecker {
3606236062
const len = signature.parameters.length - (signatureHasRestParameter(signature) ? 1 : 0);
3606336063
for (let i = 0; i < len; i++) {
3606436064
const parameter = signature.parameters[i];
36065-
if (!getEffectiveTypeAnnotationNode(parameter.valueDeclaration as ParameterDeclaration)) {
36066-
const contextualParameterType = tryGetTypeAtPosition(context, i);
36067-
assignParameterType(parameter, contextualParameterType);
36065+
const declaration = parameter.valueDeclaration as ParameterDeclaration;
36066+
if (!getEffectiveTypeAnnotationNode(declaration)) {
36067+
let type = tryGetTypeAtPosition(context, i);
36068+
if (type && type.flags & TypeFlags.Never && declaration.initializer) {
36069+
type = widenTypeInferredFromInitializer(declaration, checkDeclarationInitializer(declaration, CheckMode.Normal));
36070+
}
36071+
assignParameterType(parameter, type);
3606836072
}
3606936073
}
3607036074
if (signatureHasRestParameter(signature)) {
Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
//// [tests/cases/compiler/contextuallyTypedNeverParameterWithInitializer.ts] ////
2+
3+
=== contextuallyTypedNeverParameterWithInitializer.ts ===
4+
declare function test1<
5+
>test1 : Symbol(test1, Decl(contextuallyTypedNeverParameterWithInitializer.ts, 0, 0))
6+
7+
TContext,
8+
>TContext : Symbol(TContext, Decl(contextuallyTypedNeverParameterWithInitializer.ts, 0, 23))
9+
10+
TMethods extends Record<string, (ctx: TContext, ...args: never[]) => unknown>,
11+
>TMethods : Symbol(TMethods, Decl(contextuallyTypedNeverParameterWithInitializer.ts, 1, 11))
12+
>Record : Symbol(Record, Decl(lib.es5.d.ts, --, --))
13+
>ctx : Symbol(ctx, Decl(contextuallyTypedNeverParameterWithInitializer.ts, 2, 35))
14+
>TContext : Symbol(TContext, Decl(contextuallyTypedNeverParameterWithInitializer.ts, 0, 23))
15+
>args : Symbol(args, Decl(contextuallyTypedNeverParameterWithInitializer.ts, 2, 49))
16+
17+
>(context: TContext, methods: TMethods): void;
18+
>context : Symbol(context, Decl(contextuallyTypedNeverParameterWithInitializer.ts, 3, 2))
19+
>TContext : Symbol(TContext, Decl(contextuallyTypedNeverParameterWithInitializer.ts, 0, 23))
20+
>methods : Symbol(methods, Decl(contextuallyTypedNeverParameterWithInitializer.ts, 3, 20))
21+
>TMethods : Symbol(TMethods, Decl(contextuallyTypedNeverParameterWithInitializer.ts, 1, 11))
22+
23+
test1(
24+
>test1 : Symbol(test1, Decl(contextuallyTypedNeverParameterWithInitializer.ts, 0, 0))
25+
{
26+
count: 0,
27+
>count : Symbol(count, Decl(contextuallyTypedNeverParameterWithInitializer.ts, 6, 3))
28+
29+
},
30+
{
31+
checkLimit: (ctx, max = 500) => {},
32+
>checkLimit : Symbol(checkLimit, Decl(contextuallyTypedNeverParameterWithInitializer.ts, 9, 3))
33+
>ctx : Symbol(ctx, Decl(contextuallyTypedNeverParameterWithInitializer.ts, 10, 17))
34+
>max : Symbol(max, Decl(contextuallyTypedNeverParameterWithInitializer.ts, 10, 21))
35+
36+
hasAccess: (ctx, user: { name: string }) => {},
37+
>hasAccess : Symbol(hasAccess, Decl(contextuallyTypedNeverParameterWithInitializer.ts, 10, 39))
38+
>ctx : Symbol(ctx, Decl(contextuallyTypedNeverParameterWithInitializer.ts, 11, 16))
39+
>user : Symbol(user, Decl(contextuallyTypedNeverParameterWithInitializer.ts, 11, 20))
40+
>name : Symbol(name, Decl(contextuallyTypedNeverParameterWithInitializer.ts, 11, 28))
41+
42+
},
43+
);
Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
//// [tests/cases/compiler/contextuallyTypedNeverParameterWithInitializer.ts] ////
2+
3+
=== contextuallyTypedNeverParameterWithInitializer.ts ===
4+
declare function test1<
5+
>test1 : <TContext, TMethods extends Record<string, (ctx: TContext, ...args: never[]) => unknown>>(context: TContext, methods: TMethods) => void
6+
7+
TContext,
8+
TMethods extends Record<string, (ctx: TContext, ...args: never[]) => unknown>,
9+
>ctx : TContext
10+
>args : never[]
11+
12+
>(context: TContext, methods: TMethods): void;
13+
>context : TContext
14+
>methods : TMethods
15+
16+
test1(
17+
>test1( { count: 0, }, { checkLimit: (ctx, max = 500) => {}, hasAccess: (ctx, user: { name: string }) => {}, },) : void
18+
>test1 : <TContext, TMethods extends Record<string, (ctx: TContext, ...args: never[]) => 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 = 500) => {}, hasAccess: (ctx, user: { name: string }) => {}, } : { checkLimit: (ctx: { count: number; }, max?: number) => void; hasAccess: (ctx: { count: number; }, user: { name: string;}) => void; }
29+
30+
checkLimit: (ctx, max = 500) => {},
31+
>checkLimit : (ctx: { count: number; }, max?: number) => void
32+
>(ctx, max = 500) => {} : (ctx: { count: number; }, max?: number) => void
33+
>ctx : { count: number; }
34+
>max : number
35+
>500 : 500
36+
37+
hasAccess: (ctx, user: { name: string }) => {},
38+
>hasAccess : (ctx: { count: number; }, user: { name: string;}) => void
39+
>(ctx, user: { name: string }) => {} : (ctx: { count: number; }, user: { name: string;}) => void
40+
>ctx : { count: number; }
41+
>user : { name: string; }
42+
>name : string
43+
44+
},
45+
);
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
// @strict: true
2+
// @noEmit: true
3+
4+
declare function test1<
5+
TContext,
6+
TMethods extends Record<string, (ctx: TContext, ...args: never[]) => unknown>,
7+
>(context: TContext, methods: TMethods): void;
8+
9+
test1(
10+
{
11+
count: 0,
12+
},
13+
{
14+
checkLimit: (ctx, max = 500) => {},
15+
hasAccess: (ctx, user: { name: string }) => {},
16+
},
17+
);

0 commit comments

Comments
 (0)