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

New Type: AllUnionFields (similar to SharedUnionFields) #996

Open
1 task done
RebeccaStevens opened this issue Dec 3, 2024 · 2 comments · May be fixed by #997
Open
1 task done

New Type: AllUnionFields (similar to SharedUnionFields) #996

RebeccaStevens opened this issue Dec 3, 2024 · 2 comments · May be fixed by #997

Comments

@RebeccaStevens
Copy link
Contributor

RebeccaStevens commented Dec 3, 2024

Type description + examples

Expand on SharedUnionFields with a type that keeps all the union fields.
This is a simplified version of #386.

This new type will simply change non-shared fields to optional with value unknown. Or with an option (proposed name exact), will be changed to be optional with a value of the union of all values for that property within the union.

Edit: This new type will change each field to be optional with a value of the union of all values for that property within the union.

Simple example by modifying the SharedUnionFields's example (probably not that great an example of why you'd actually want to use this type)

import type {AllUnionFields} from 'type-fest';

type Cat = {
	name: string;
	type: 'cat';
	catType: string;
};

type Dog = {
	name: string;
	type: 'dog';
	dogType: string;
};

function displayPetInfo(petInfo: Cat | Dog) {
	// typeof petInfo =>
	// {
	// 	name: string;
	// 	type: 'cat';
	// 	catType: string;
	// } | {
	// 	name: string;
	// 	type: 'dog';
	// 	dogType: string;
	// }

	console.log('name: ', petInfo.name);
	console.log('type: ', petInfo.type);

	// TypeScript complains about `catType` and `dogType` not existing on type `Cat | Dog`.
	console.log('animal type: ', petInfo.catType ?? petInfo.dogType);
}

function displayPetInfo(petInfo: AllUnionFields<Cat | Dog>) {
	// typeof petInfo =>
	// {
	// 	name: string;
	// 	type: 'cat' | 'dog';
	//  catType?: string;
	//  dogType?: string;
	// }

	console.log('name: ', petInfo.name);
	console.log('type: ', petInfo.type);

	// No TypeScript error.
	console.log('animal type: ', petInfo.catType ?? petInfo.dogType);
}

Type source

No response

Search existing types and issues first

  • I tried my best to look for it

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
@RebeccaStevens RebeccaStevens linked a pull request Dec 3, 2024 that will close this issue
@Emiyaaaaa
Copy link
Collaborator

Emiyaaaaa commented Dec 3, 2024

I think this result is better (don't need a option to choose unknown or exact type):

type petinfo = {
 name: string;
 type: 'cat' | 'dog';
 catType?: string;
 dogType?: string;
}

I'm not sure if I'm missing it, I don't find the similar type in type-fest, I think it's a useful type (need a better type name).

@RebeccaStevens
Copy link
Contributor Author

After thinking about this a bit more, I think I was being overly cautious with the exact option opt-in. I think that should probably be just the way it works.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants