Skip to content
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 types on "this" in instance methods #13800

Closed
1 task done
ozzyonfire opened this issue Aug 31, 2023 · 8 comments
Closed
1 task done

Typescript types on "this" in instance methods #13800

ozzyonfire opened this issue Aug 31, 2023 · 8 comments
Labels
typescript Types or Types-test related issue / Pull Request
Milestone

Comments

@ozzyonfire
Copy link

Prerequisites

  • I have written a descriptive issue title

Mongoose version

7.5.0

Node.js version

18.x

MongoDB version

5.x

Operating system

macOS

Operating system version (i.e. 20.04, 11.3, 10)

13.5

Issue

I am trying to get Typescript typings to work on instance methods defined on my schema. I've followed along in the docs, but I can only get types working on "this" that reflect methods, and not any properties.

Example schema (I pulled from a working example I found on here):

import { Model, Schema, model } from 'mongoose';

interface IUser {
	firstName: string;
	lastName: string;
}

// Put all user instance methods in this interface:
interface IUserMethods {
	fullName(): string;
}

// Create a new Model type that knows about IUserMethods...
type UserModel = Model<IUser, {}, IUserMethods>;

// And a schema that knows about IUserMethods
const schema = new Schema<IUser, UserModel, IUserMethods>({
	firstName: { type: String, required: true },
	lastName: { type: String, required: true },
});

schema.method('fullName', function fullName() {
	return this.firstName + ' ' + this.lastName;
});

const User = model<IUser, UserModel>('User', schema);

const user = new User({ firstName: 'Jean-Luc', lastName: 'Picard' });
user.firstName; // the typings here work great
const fullName: string = user.fullName(); // 'Jean-Luc Picard'

But my typescript doesn't give me the proper type of "this", "firstName" or "lastName"...
image

I am upgrading my project from an older version of mongoose, typescript, etc. and I swear I used to have proper property types on this.

Typescript version: 5.2.2
Node version 18 and my tsconfig is set up with "strict": true

{
	"extends": "@tsconfig/node18/tsconfig.json",
	"compilerOptions": {
		"strict": true,
		"strictNullChecks": true,
		"allowSyntheticDefaultImports": true,
		"esModuleInterop": true,
	}
}

Thanks for any insight!

@ozzyonfire ozzyonfire added help This issue can likely be resolved in GitHub issues. No bug fixes, features, or docs necessary help wanted labels Aug 31, 2023
@vkarpov15 vkarpov15 added this to the 7.5.1 milestone Sep 4, 2023
@pshaddel
Copy link
Contributor

pshaddel commented Sep 4, 2023

I made this PR to fix this: #13822

@valeriuflorescu16
Copy link
Contributor

I think you could update your schema as follows:

const schema = new Schema<IUser, UserModel, IUserMethods>({
	firstName: { type: String, required: true },
	lastName: { type: String, required: true },
}, {
	methods: {
		getFullName() {
			return `${this.firstName} ${this.lastName}`;
		}
	}
});

This way, the typing should work just fine. You can then delete the following:

schema.method('fullName', function fullName() {
	return this.firstName + ' ' + this.lastName;
});

Not sure if that works but it should I think.

@vkarpov15
Copy link
Collaborator

Fixed by #13822

@vkarpov15 vkarpov15 added typescript Types or Types-test related issue / Pull Request and removed help This issue can likely be resolved in GitHub issues. No bug fixes, features, or docs necessary help wanted labels Sep 8, 2023
@ozzyonfire
Copy link
Author

I'm glad to see that this was fixed, but when will this be available in an official release? What is the release schedule like? Should this be marked closed if it's still not available in an official release?

@ozzyonfire
Copy link
Author

Okay nevermind, I see that this is scheduled to release in 7.5.1 which looks like it's almost ready... I was just excited to have this in my project, but then wasn't sure when npm update didn't do anything.

Thanks again for fixing this quickly!

@ruxxzebre
Copy link
Contributor

Still having this issue, when using "methods" field on schema. Would be grateful for help!
What works, is that if we remove methods from Schema generic, this will be typed as a document, but without my own declared methods.

  1. providing IUserMethods
Screenshot 2023-11-01 at 19 08 34 2) w/o IUserMethods Screenshot 2023-11-01 at 19 10 09

@pshaddel
Copy link
Contributor

pshaddel commented Nov 1, 2023

@ruxxzebre I think this is another issue with methods. Could you open a new issue and add this code to the new issue? If you tag me I could take a look into this issue.

@ruxxzebre
Copy link
Contributor

ruxxzebre commented Nov 1, 2023

Will do. Thanks!
Edit: created a new issue @pshaddel

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
typescript Types or Types-test related issue / Pull Request
Projects
None yet
Development

No branches or pull requests

5 participants