Skip to content

Combine multiple overloads into a single contextual signature #42620

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
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
159 changes: 134 additions & 25 deletions src/compiler/checker.ts

Large diffs are not rendered by default.

4 changes: 3 additions & 1 deletion src/compiler/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5548,7 +5548,9 @@ namespace ts {
/* @internal */
mapper?: TypeMapper; // Instantiation mapper
/* @internal */
unionSignatures?: Signature[]; // Underlying signatures of a union signature
compositeSignatures?: Signature[]; // Underlying signatures of a union/intersection signature
/* @internal */
compositeKind?: TypeFlags; // TypeFlags.Union if the underlying signatures are from union members, otherwise TypeFlags.Intersection
/* @internal */
erasedSignatureCache?: Signature; // Erased version of signature (deferred)
/* @internal */
Expand Down
110 changes: 0 additions & 110 deletions tests/baselines/reference/callsOnComplexSignatures.errors.txt

This file was deleted.

6 changes: 3 additions & 3 deletions tests/baselines/reference/callsOnComplexSignatures.types
Original file line number Diff line number Diff line change
Expand Up @@ -100,13 +100,13 @@ function test3(items: string[] | number[]) {
>items.forEach : ((callbackfn: (value: string, index: number, array: string[]) => void, thisArg?: any) => void) | ((callbackfn: (value: number, index: number, array: number[]) => void, thisArg?: any) => void)
>items : string[] | number[]
>forEach : ((callbackfn: (value: string, index: number, array: string[]) => void, thisArg?: any) => void) | ((callbackfn: (value: number, index: number, array: number[]) => void, thisArg?: any) => void)
>item => console.log(item) : (item: any) => void
>item : any
>item => console.log(item) : (item: string | number) => void
>item : string | number
>console.log(item) : void
>console.log : (...data: any[]) => void
>console : Console
>log : (...data: any[]) => void
>item : any
>item : string | number
}

function test4(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ declare var connect: Connect;

const myStoreConnect: Connect = function(
>myStoreConnect : Connect
>function( mapStateToProps?: any, mapDispatchToProps?: any, mergeProps?: any, options: unknown = {},) { return connect( mapStateToProps, mapDispatchToProps, mergeProps, options, );} : (mapStateToProps?: any, mapDispatchToProps?: any, mergeProps?: any, options?: unknown) => InferableComponentEnhancerWithProps<unknown, unknown>
>function( mapStateToProps?: any, mapDispatchToProps?: any, mergeProps?: any, options: unknown = {},) { return connect( mapStateToProps, mapDispatchToProps, mergeProps, options, );} : <TStateProps, TOwnProps>(mapStateToProps?: any, mapDispatchToProps?: any, mergeProps?: any, options?: unknown) => InferableComponentEnhancerWithProps<TStateProps, Omit<P, Extract<keyof TStateProps, keyof P>> & TOwnProps>

mapStateToProps?: any,
>mapStateToProps : any
Expand All @@ -73,7 +73,7 @@ const myStoreConnect: Connect = function(

) {
return connect(
>connect( mapStateToProps, mapDispatchToProps, mergeProps, options, ) : InferableComponentEnhancerWithProps<unknown, unknown>
>connect( mapStateToProps, mapDispatchToProps, mergeProps, options, ) : InferableComponentEnhancerWithProps<TStateProps, Omit<P, Extract<keyof TStateProps, keyof P>> & TOwnProps>
>connect : Connect

mapStateToProps,
Expand Down
111 changes: 111 additions & 0 deletions tests/baselines/reference/contextualOverloadListFromArrayUnion.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,111 @@
//// [tests/cases/compiler/contextualOverloadListFromArrayUnion.ts] ////

//// [one.ts]
declare const y: never[] | string[];
export const yThen = y.map(item => item.length);
//// [two.ts]
declare const y: number[][] | string[];
export const yThen = y.map(item => item.length);
//// [three.ts]
// #42504
interface ResizeObserverCallback {
(entries: ResizeObserverEntry[], observer: ResizeObserver): void;
}
interface ResizeObserverCallback { // duplicate for effect
(entries: ResizeObserverEntry[], observer: ResizeObserver): void;
}

const resizeObserver = new ResizeObserver(([entry]) => {
entry
});
// comment in #35501
interface Callback<T> {
(error: null, result: T): unknown
(error: Error, result: null): unknown
}

interface Task<T> {
(callback: Callback<T>): unknown
}

export function series<T>(tasks: Task<T>[], callback: Callback<T[]>): void {
let index = 0
let results: T[] = []

function next() {
let task = tasks[index]
if (!task) {
callback(null, results)
} else {
task((error, result) => {
if (error) {
callback(error, null)
} else {
// must use postfix-!, since `error` and `result` don't have a
// causal relationship when the overloads are combined
results.push(result!)
next()
}
})
}
}
next()
}

series([
cb => setTimeout(() => cb(null, 1), 300),
cb => setTimeout(() => cb(null, 2), 200),
cb => setTimeout(() => cb(null, 3), 100),
], (error, results) => {
if (error) {
console.error(error)
} else {
console.log(results)
}
})


//// [one.js]
export const yThen = y.map(item => item.length);
//// [two.js]
export const yThen = y.map(item => item.length);
//// [three.js]
const resizeObserver = new ResizeObserver(([entry]) => {
entry;
});
export function series(tasks, callback) {
let index = 0;
let results = [];
function next() {
let task = tasks[index];
if (!task) {
callback(null, results);
}
else {
task((error, result) => {
if (error) {
callback(error, null);
}
else {
// must use postfix-!, since `error` and `result` don't have a
// causal relationship when the overloads are combined
results.push(result);
next();
}
});
}
}
next();
}
series([
cb => setTimeout(() => cb(null, 1), 300),
cb => setTimeout(() => cb(null, 2), 200),
cb => setTimeout(() => cb(null, 3), 100),
], (error, results) => {
if (error) {
console.error(error);
}
else {
console.log(results);
}
});
Loading