Improve Repository.save() return type#2796
Merged
pleerock merged 1 commit intotypeorm:masterfrom Nov 24, 2018
Merged
Conversation
Member
|
Thanks for contribution! by making it |
Contributor
Author
That's why I define those nullable properties as TypeScript optional types. See the prop With my change, the following is true, because @Entity()
export default class User {
@PrimaryGeneratedColumn('uuid') id: string
@Column() name: string
@Column({ nullable: true }) address?: string
@Column({ nullable: true }) age?: number
}
// ...
const user = { name: 'John Doe', address: 'Somewhere' }
const createdUser = await this.repository.save(user)
createdUser.address // okay
createdUser.age // error, might be undefined |
Member
|
okay, lets merge it, and lets see how people react to this change. Thank you for the contribution! |
7 tasks
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
When saving a new entity with
repository.save(fields), the current type definition fails to return to me the right fields types for the generated columns, for example:This seems to only make sense when
reload: falseis sent, which is not the case.This little change fixes that, while overriding the optional type when the values are present.
I'm not sure if this breaks some special case, maybe some maintainer could look into this 😅