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

Proposal: SetFieldType #718

Closed
lucasteles opened this issue Oct 20, 2023 · 8 comments · Fixed by #721
Closed

Proposal: SetFieldType #718

lucasteles opened this issue Oct 20, 2023 · 8 comments · Fixed by #721
Labels
help wanted Extra attention is needed type addition

Comments

@lucasteles
Copy link
Contributor

lucasteles commented Oct 20, 2023

A simple shallow field type replace utility type

definition:

import type {Except} from './except';
import type {Simplify} from './simplify';

export type SetFieldType<BaseType, Keys extends keyof BaseType, NewType> =
  Simplify<
  Except<BaseType, Keys> &
  Record<Keys, NewType>
  >;

use case:

type MyModel = { id: number; createdAt: Date; updatedAt: Date; enabled: boolean }
type MyModelApi = SetFieldType<MyModel, 'createdAt' | 'updatedAt', string>
// MyModelApi == { id: number; createdAt: string; updatedAt: string; enabled: boolean }

const response = fetch<MyModelApi>("url")

Upvote & Fund

  • We're using Polar.sh so you can upvote and help fund this issue.
  • The funding will be given to active contributors.
  • Thank you in advance for helping prioritize & fund our backlog.
Fund with Polar
@sindresorhus
Copy link
Owner

I would name it SetFieldType.

@sindresorhus
Copy link
Owner

Related type: #675

@sindresorhus sindresorhus added help wanted Extra attention is needed type addition labels Oct 20, 2023
@lucasteles
Copy link
Contributor Author

@sindresorhus looks great, i will update the issue

@lucasteles lucasteles changed the title Proposal: SetReplace Proposal: SetFieldType Oct 20, 2023
@Emiyaaaaa
Copy link
Collaborator

This type seem like type Overwrite<T, K> = Omit<T, keyof K> & K
Maybe Overwrite<MyModel, {createdAt: string, updatedAt: string}> is more universal?

@sindresorhus Thoughts?

@sindresorhus
Copy link
Owner

Maybe Overwrite<MyModel, {createdAt: string, updatedAt: string}> is more universal?

Yeah, I like it better. More flexible as you can replace multiple properties with different types at once.

@Emiyaaaaa
Copy link
Collaborator

Emiyaaaaa commented Oct 21, 2023

Yeah, I like it better. More flexible as you can replace multiple properties with different types at once.

I found Merge is same to Overwrite in this case. can just use Merge before having the case that Overwrite is better than Merge

@lucasteles
Copy link
Contributor Author

I believe overwrite/merge is not quite what I am aiming for here

I have used this approach before and I figured myself having to repeat the structure and the same type multiple times which made me create this helper, to easily change multiple fields to one single type

@sindresorhus
Copy link
Owner

Thinking more about this, I think we can have both types.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
help wanted Extra attention is needed type addition
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants