Skip to content

SchemaTypeOptions is inaccessible with a pre-defined interface (typescript) #9911

@ShadiestGoat

Description

@ShadiestGoat

Do you want to request a feature or report a bug?

bug

What is the current behavior?

You are unable to reach the SchemaTypeOptions if using a pre-made interface.

If the current behavior is a bug, please provide the steps to reproduce.

export interface IUser { 
    username: string,
    password: string,
}
type SH = IUser & Document

const SH = new Schema<SH, Model<SH>, SH>({
    username: {unique: true, type: String},
    password: String,
})

username: {unique: true, type: String}
Gives of an error since its not String (not SchemaDefinitionProperty<string>)

Also, if I would try to do something like export type SH = Omit<IUser, "username"> & Document & {username: SchemaTypeOptions<String>}, then i would get an error that

Type '{ unique: true; type: StringConstructor; }' is not assignable to type 'SchemaDefinitionProperty<SchemaTypeOptions<String>>'. Type '{ unique: true; type: StringConstructor; }' is not assignable to type 'string'

tsconfig.json is irrelevant

What is the expected behavior?

That you are able to get to SchemaTypeOptions, somehow.

What are the versions of Node.js, Mongoose and MongoDB you are using? Note that "latest" is not a version.

5.11.15

Edit:
Technically, you can access it, through the use of

type SH = Omit<IUser, "username"> & Document & {username: any}

But that feels like a very work around-ish way to do it. Is there a better way for this? Because i see that the type definition for the SchemaTypeOptions having a generics type there, so I assume there was intended to be a way for it?

Metadata

Metadata

Assignees

Labels

typescriptTypes or Types-test related issue / Pull Request

Type

No type

Projects

No projects

Milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions