-
-
Notifications
You must be signed in to change notification settings - Fork 3.8k
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
TypeScript - Importing Mongoose makes string extend Function in 6.0.6 #10746
Comments
I have no idea why, but this is caused by change on line 1530 in this commit: e0fb110 , and adding back |
I tried to create a minimal repro, but no luck. I think the way to go would be to create a simplified version of Mongoose's declare module 'mongoose' {
class Schema<DocType = any, M = any, TInstanceMethods = {}> {
constructor(definition?: SchemaDefinition<DocType>, options?: any);
}
class SchemaType {}
namespace Schema {
namespace Types {
class String extends SchemaType {}
}
}
class NativeDate extends Date {}
type SchemaDefinitionProperty<T = undefined> = SchemaDefinitionWithBuiltInClass<T> |
SchemaTypeOptions<T extends undefined ? any : T> |
Schema<any, any, any> |
Schema<any, any, any>[] |
SchemaTypeOptions<T extends undefined ? any : T>[] |
Function[] |
SchemaDefinition<T> |
SchemaDefinition<T>[];
type SchemaDefinition<T = undefined> = T extends undefined
? { [path: string]: SchemaDefinitionProperty; }
: { [path in keyof T]?: SchemaDefinitionProperty<T[path]>; };
type SchemaDefinitionWithBuiltInClass<T> = T extends number
? (typeof Number | 'number' | 'Number')
: T extends string
? (typeof String | 'string' | 'String' | typeof Schema.Types.String)
: T extends boolean
? (typeof Boolean | 'boolean' | 'Boolean')
: T extends NativeDate
? (typeof NativeDate | 'date' | 'Date')
: (Function | string);
type Unpacked<T> = T extends (infer U)[] ?
U :
T extends ReadonlyArray<infer U> ? U : T;
type AnyArray<T> = T[] | ReadonlyArray<T>;
export class SchemaTypeOptions<T> {
type?:
T extends string | number | boolean | NativeDate | Function ? SchemaDefinitionWithBuiltInClass<T> :
T extends object[] ? (AnyArray<Schema<any>> | AnyArray<SchemaDefinition<Unpacked<T>>>) :
T extends string[] ? AnyArray<SchemaDefinitionWithBuiltInClass<string>> | AnyArray<SchemaTypeOptions<string>> :
T extends number[] ? AnyArray<SchemaDefinitionWithBuiltInClass<number>> | AnyArray<SchemaTypeOptions<number>> :
T extends boolean[] ? AnyArray<SchemaDefinitionWithBuiltInClass<boolean>> | AnyArray<SchemaTypeOptions<boolean>> :
T extends Function[] ? AnyArray<SchemaDefinitionWithBuiltInClass<Function>> | AnyArray<SchemaTypeOptions<Unpacked<T>>> :
T | typeof SchemaType | Schema<any, any, any> | SchemaDefinition<T>;
}
} I also found that this issue only affects TypeScript 4.3.x and 4.4.x. This issue doesn't show up in our tests because we're using TypeScript 4.1. |
Do you want to request a feature or report a bug?
Bug
What is the current behavior?
When I import mongoose
6.0.6
, the TypeScript compiler incorrectly evaluatesstring extends Function
astrue
. The issue does not occur with mongoose6.0.5
.If the current behavior is a bug, please provide the steps to reproduce.
This script can reproduce the issue: with the mongoose import, the script does not compile. If the mongoose import is removed, the script compiles.
tsconfig.json
:What is the expected behavior?
string
should not extendFunction
.What are the versions of Node.js, Mongoose and MongoDB you are using? Note that "latest" is not a version.
Node:
v14.16.1
TypeScript:
4.4.3
Mongoose:
v6.0.6
MongoDB:
v4.1.2
The text was updated successfully, but these errors were encountered: