Skip to content

Commit d9c56f2

Browse files
committed
Fixed inferred const rest types in signatures
1 parent 8a85b2a commit d9c56f2

5 files changed

+208
-5
lines changed

src/compiler/checker.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -23925,7 +23925,7 @@ export function createTypeChecker(host: TypeCheckerHost): TypeChecker {
2392523925
callback(getTypeAtPosition(source, i), getTypeAtPosition(target, i));
2392623926
}
2392723927
if (targetRestType) {
23928-
callback(getRestTypeAtPosition(source, paramCount), targetRestType);
23928+
callback(getRestTypeAtPosition(source, paramCount, /*readonly*/ isConstTypeVariable(targetRestType)), targetRestType);
2392923929
}
2393023930
}
2393123931

@@ -34928,7 +34928,7 @@ export function createTypeChecker(host: TypeCheckerHost): TypeChecker {
3492834928
return undefined;
3492934929
}
3493034930

34931-
function getRestTypeAtPosition(source: Signature, pos: number): Type {
34931+
function getRestTypeAtPosition(source: Signature, pos: number, readonly?: boolean): Type {
3493234932
const parameterCount = getParameterCount(source);
3493334933
const minArgumentCount = getMinArgumentCount(source);
3493434934
const restType = getEffectiveRestType(source);
@@ -34952,7 +34952,7 @@ export function createTypeChecker(host: TypeCheckerHost): TypeChecker {
3495234952
names.push(name);
3495334953
}
3495434954
}
34955-
return createTupleType(types, flags, /*readonly*/ false, length(names) === length(types) ? names : undefined);
34955+
return createTupleType(types, flags, readonly, length(names) === length(types) ? names : undefined);
3495634956
}
3495734957

3495834958
// Return the number of parameters in a signature. The rest parameter, if present, counts as one

tests/baselines/reference/typeParameterConstModifiers.errors.txt

Lines changed: 24 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -107,4 +107,27 @@ typeParameterConstModifiers.ts(55,9): error TS1277: 'const' modifier can only ap
107107
const thingMapped = <const O extends Record<string, any>>(o: NotEmptyMapped<O>) => o;
108108

109109
const tMapped = thingMapped({ foo: '' }); // { foo: "" }
110-
110+
111+
// repro from https://github.com/microsoft/TypeScript/issues/55033
112+
113+
function factory_55033_minimal<const T extends readonly unknown[]>(cb: (...args: T) => void) {
114+
return {} as T
115+
}
116+
117+
const test_55033_minimal = factory_55033_minimal((b: string) => {})
118+
119+
function factory_55033<const T extends readonly unknown[]>(cb: (...args: T) => void) {
120+
return function call<const K extends T>(...args: K): K {
121+
return {} as K;
122+
};
123+
}
124+
125+
const t1_55033 = factory_55033((a: { test: number }, b: string) => {})(
126+
{ test: 123 },
127+
"some string"
128+
);
129+
130+
const t2_55033 = factory_55033((a: { test: number }, b: string) => {})(
131+
{ test: 123 } as const,
132+
"some string"
133+
);

tests/baselines/reference/typeParameterConstModifiers.js

Lines changed: 40 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -101,7 +101,30 @@ type NotEmptyMapped<T extends Record<string, any>> = keyof T extends never ? nev
101101
const thingMapped = <const O extends Record<string, any>>(o: NotEmptyMapped<O>) => o;
102102

103103
const tMapped = thingMapped({ foo: '' }); // { foo: "" }
104-
104+
105+
// repro from https://github.com/microsoft/TypeScript/issues/55033
106+
107+
function factory_55033_minimal<const T extends readonly unknown[]>(cb: (...args: T) => void) {
108+
return {} as T
109+
}
110+
111+
const test_55033_minimal = factory_55033_minimal((b: string) => {})
112+
113+
function factory_55033<const T extends readonly unknown[]>(cb: (...args: T) => void) {
114+
return function call<const K extends T>(...args: K): K {
115+
return {} as K;
116+
};
117+
}
118+
119+
const t1_55033 = factory_55033((a: { test: number }, b: string) => {})(
120+
{ test: 123 },
121+
"some string"
122+
);
123+
124+
const t2_55033 = factory_55033((a: { test: number }, b: string) => {})(
125+
{ test: 123 } as const,
126+
"some string"
127+
);
105128

106129
//// [typeParameterConstModifiers.js]
107130
"use strict";
@@ -154,3 +177,19 @@ var thing = function (o) { return o; };
154177
var t = thing({ foo: '' }); // readonly { foo: "" }
155178
var thingMapped = function (o) { return o; };
156179
var tMapped = thingMapped({ foo: '' }); // { foo: "" }
180+
// repro from https://github.com/microsoft/TypeScript/issues/55033
181+
function factory_55033_minimal(cb) {
182+
return {};
183+
}
184+
var test_55033_minimal = factory_55033_minimal(function (b) { });
185+
function factory_55033(cb) {
186+
return function call() {
187+
var args = [];
188+
for (var _i = 0; _i < arguments.length; _i++) {
189+
args[_i] = arguments[_i];
190+
}
191+
return {};
192+
};
193+
}
194+
var t1_55033 = factory_55033(function (a, b) { })({ test: 123 }, "some string");
195+
var t2_55033 = factory_55033(function (a, b) { })({ test: 123 }, "some string");

tests/baselines/reference/typeParameterConstModifiers.symbols

Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -361,3 +361,68 @@ const tMapped = thingMapped({ foo: '' }); // { foo: "" }
361361
>thingMapped : Symbol(thingMapped, Decl(typeParameterConstModifiers.ts, 97, 5))
362362
>foo : Symbol(foo, Decl(typeParameterConstModifiers.ts, 99, 29))
363363

364+
// repro from https://github.com/microsoft/TypeScript/issues/55033
365+
366+
function factory_55033_minimal<const T extends readonly unknown[]>(cb: (...args: T) => void) {
367+
>factory_55033_minimal : Symbol(factory_55033_minimal, Decl(typeParameterConstModifiers.ts, 99, 41))
368+
>T : Symbol(T, Decl(typeParameterConstModifiers.ts, 103, 31))
369+
>cb : Symbol(cb, Decl(typeParameterConstModifiers.ts, 103, 67))
370+
>args : Symbol(args, Decl(typeParameterConstModifiers.ts, 103, 72))
371+
>T : Symbol(T, Decl(typeParameterConstModifiers.ts, 103, 31))
372+
373+
return {} as T
374+
>T : Symbol(T, Decl(typeParameterConstModifiers.ts, 103, 31))
375+
}
376+
377+
const test_55033_minimal = factory_55033_minimal((b: string) => {})
378+
>test_55033_minimal : Symbol(test_55033_minimal, Decl(typeParameterConstModifiers.ts, 107, 5))
379+
>factory_55033_minimal : Symbol(factory_55033_minimal, Decl(typeParameterConstModifiers.ts, 99, 41))
380+
>b : Symbol(b, Decl(typeParameterConstModifiers.ts, 107, 50))
381+
382+
function factory_55033<const T extends readonly unknown[]>(cb: (...args: T) => void) {
383+
>factory_55033 : Symbol(factory_55033, Decl(typeParameterConstModifiers.ts, 107, 67))
384+
>T : Symbol(T, Decl(typeParameterConstModifiers.ts, 109, 23))
385+
>cb : Symbol(cb, Decl(typeParameterConstModifiers.ts, 109, 59))
386+
>args : Symbol(args, Decl(typeParameterConstModifiers.ts, 109, 64))
387+
>T : Symbol(T, Decl(typeParameterConstModifiers.ts, 109, 23))
388+
389+
return function call<const K extends T>(...args: K): K {
390+
>call : Symbol(call, Decl(typeParameterConstModifiers.ts, 110, 10))
391+
>K : Symbol(K, Decl(typeParameterConstModifiers.ts, 110, 25))
392+
>T : Symbol(T, Decl(typeParameterConstModifiers.ts, 109, 23))
393+
>args : Symbol(args, Decl(typeParameterConstModifiers.ts, 110, 44))
394+
>K : Symbol(K, Decl(typeParameterConstModifiers.ts, 110, 25))
395+
>K : Symbol(K, Decl(typeParameterConstModifiers.ts, 110, 25))
396+
397+
return {} as K;
398+
>K : Symbol(K, Decl(typeParameterConstModifiers.ts, 110, 25))
399+
400+
};
401+
}
402+
403+
const t1_55033 = factory_55033((a: { test: number }, b: string) => {})(
404+
>t1_55033 : Symbol(t1_55033, Decl(typeParameterConstModifiers.ts, 115, 5))
405+
>factory_55033 : Symbol(factory_55033, Decl(typeParameterConstModifiers.ts, 107, 67))
406+
>a : Symbol(a, Decl(typeParameterConstModifiers.ts, 115, 32))
407+
>test : Symbol(test, Decl(typeParameterConstModifiers.ts, 115, 36))
408+
>b : Symbol(b, Decl(typeParameterConstModifiers.ts, 115, 52))
409+
410+
{ test: 123 },
411+
>test : Symbol(test, Decl(typeParameterConstModifiers.ts, 116, 5))
412+
413+
"some string"
414+
);
415+
416+
const t2_55033 = factory_55033((a: { test: number }, b: string) => {})(
417+
>t2_55033 : Symbol(t2_55033, Decl(typeParameterConstModifiers.ts, 120, 5))
418+
>factory_55033 : Symbol(factory_55033, Decl(typeParameterConstModifiers.ts, 107, 67))
419+
>a : Symbol(a, Decl(typeParameterConstModifiers.ts, 120, 32))
420+
>test : Symbol(test, Decl(typeParameterConstModifiers.ts, 120, 36))
421+
>b : Symbol(b, Decl(typeParameterConstModifiers.ts, 120, 52))
422+
423+
{ test: 123 } as const,
424+
>test : Symbol(test, Decl(typeParameterConstModifiers.ts, 121, 5))
425+
>const : Symbol(const)
426+
427+
"some string"
428+
);

tests/baselines/reference/typeParameterConstModifiers.types

Lines changed: 76 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -409,3 +409,79 @@ const tMapped = thingMapped({ foo: '' }); // { foo: "" }
409409
>foo : ""
410410
>'' : ""
411411

412+
// repro from https://github.com/microsoft/TypeScript/issues/55033
413+
414+
function factory_55033_minimal<const T extends readonly unknown[]>(cb: (...args: T) => void) {
415+
>factory_55033_minimal : <const T extends readonly unknown[]>(cb: (...args: T) => void) => T
416+
>cb : (...args: T) => void
417+
>args : T
418+
419+
return {} as T
420+
>{} as T : T
421+
>{} : {}
422+
}
423+
424+
const test_55033_minimal = factory_55033_minimal((b: string) => {})
425+
>test_55033_minimal : readonly [b: string]
426+
>factory_55033_minimal((b: string) => {}) : readonly [b: string]
427+
>factory_55033_minimal : <const T extends readonly unknown[]>(cb: (...args: T) => void) => T
428+
>(b: string) => {} : (b: string) => void
429+
>b : string
430+
431+
function factory_55033<const T extends readonly unknown[]>(cb: (...args: T) => void) {
432+
>factory_55033 : <const T extends readonly unknown[]>(cb: (...args: T) => void) => <const K extends T>(...args: K) => K
433+
>cb : (...args: T) => void
434+
>args : T
435+
436+
return function call<const K extends T>(...args: K): K {
437+
>function call<const K extends T>(...args: K): K { return {} as K; } : <const K extends T>(...args: K) => K
438+
>call : <const K extends T>(...args: K) => K
439+
>args : K
440+
441+
return {} as K;
442+
>{} as K : K
443+
>{} : {}
444+
445+
};
446+
}
447+
448+
const t1_55033 = factory_55033((a: { test: number }, b: string) => {})(
449+
>t1_55033 : readonly [{ readonly test: 123; }, "some string"]
450+
>factory_55033((a: { test: number }, b: string) => {})( { test: 123 }, "some string") : readonly [{ readonly test: 123; }, "some string"]
451+
>factory_55033((a: { test: number }, b: string) => {}) : <const K extends readonly [a: { test: number; }, b: string]>(...args: K) => K
452+
>factory_55033 : <const T extends readonly unknown[]>(cb: (...args: T) => void) => <const K extends T>(...args: K) => K
453+
>(a: { test: number }, b: string) => {} : (a: { test: number;}, b: string) => void
454+
>a : { test: number; }
455+
>test : number
456+
>b : string
457+
458+
{ test: 123 },
459+
>{ test: 123 } : { test: 123; }
460+
>test : 123
461+
>123 : 123
462+
463+
"some string"
464+
>"some string" : "some string"
465+
466+
);
467+
468+
const t2_55033 = factory_55033((a: { test: number }, b: string) => {})(
469+
>t2_55033 : readonly [{ readonly test: 123; }, "some string"]
470+
>factory_55033((a: { test: number }, b: string) => {})( { test: 123 } as const, "some string") : readonly [{ readonly test: 123; }, "some string"]
471+
>factory_55033((a: { test: number }, b: string) => {}) : <const K extends readonly [a: { test: number; }, b: string]>(...args: K) => K
472+
>factory_55033 : <const T extends readonly unknown[]>(cb: (...args: T) => void) => <const K extends T>(...args: K) => K
473+
>(a: { test: number }, b: string) => {} : (a: { test: number;}, b: string) => void
474+
>a : { test: number; }
475+
>test : number
476+
>b : string
477+
478+
{ test: 123 } as const,
479+
>{ test: 123 } as const : { readonly test: 123; }
480+
>{ test: 123 } : { readonly test: 123; }
481+
>test : 123
482+
>123 : 123
483+
484+
"some string"
485+
>"some string" : "some string"
486+
487+
);

0 commit comments

Comments
 (0)