-
Notifications
You must be signed in to change notification settings - Fork 12.8k
error issued for overloaded methods with union type args and returns #12837
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
Comments
@psnider the reason this fails is that none of the overloads accept a union type and the union type is what you are passing. |
type DocumentID = string
type DataType = {}
type ObjectCallback = (error: Error, result?: DataType) => void;
type ArrayCallback = (error: Error, result?: DataType[]) => void;
type ObjectOrArrayCallback = (error: Error, result?: DataType | DataType[]) => void;
export class Read {
get(_id: DocumentID): DataType { return {}; }
// These functions take either a single id, and return a single object,
// or take an array of ids, and return an array of objects.
read(_ids: DocumentID[]): Promise<DataType[]>
read(_ids:DocumentID| DocumentID[], done: ArrayCallback): void
read(_id: DocumentID): Promise<DataType>
read(_id_or_ids: DocumentID | DocumentID[], done?: ObjectOrArrayCallback): Promise<DataType> | Promise<DataType[]> | void {
if (done) {
if (Array.isArray(_id_or_ids)) {
done(undefined, [{}, {}]);
} else if ((typeof _id_or_ids === 'string') && (_id_or_ids.length > 0)) {
done(undefined, {});
}
} else {
return this.promisify_read(_id_or_ids); // TS2345
}
}
// swapping the following two lines results in different error messages
promisify_read(_id: DocumentID | DocumentID[]): Promise<DataType | DataType[]>
promisify_read(_id_or_ids: DocumentID | DocumentID[]): Promise<DataType> | Promise<DataType[]> {
return new Promise((resolve, reject) => {
// TODO: resolve this typing problem
this.read(_id_or_ids, (error, result) => { // TS2345
if (!error) {
resolve(result);
} else {
reject(error);
}
});
});
}
} |
It's exactly the return type precision that I'm trying to get! Also, the change:
introduces an error, as ArrayCallback doesn't match the callback type for input of DocumentID. Do you think there is a way to keep the detailed type specifications? This problem feels like either a compiler bug, or a subtle detail about the ordering of type declarations. |
I believe this is a duplicate of #12885 |
TypeScript Version: 2.0.10, and 2.1.4
Code
Expected behavior:
I believe this code is correct, and should compile without error.
Actual behavior:
Upon compiling with: tsc --module commonjs --target es6 t.ts
the compiler outputs:
Also, if I switch the order of the first two declarations for promisify_read, the compiler changes its first error by reversing its selected types to:
Since these are all union types, I don't expect any errors.
Have I stumbled across a bug?
I'm familiar with the rule that the first matching declaration is selected, but the first two declarations select different call forms and don't overlap with each other.
Even if the first behavior is due to incorrectly declaring my functions,
the second behavior seems just wrong.
The text was updated successfully, but these errors were encountered: