Skip to content

Improve typing of Difference interface #33

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

Merged
merged 1 commit into from
Jul 27, 2022

Conversation

Schrubitteflau
Copy link
Contributor

Create 3 specialized Difference interfaces which allows us to have some type-hinting when the value of the property type is tested :

const differences: Difference[] = diff({}, {});

for (const difference of differences) {
	switch (difference.type) {
		case "CREATE":
			console.log(difference); // type-hint : DifferenceCreate
			difference.oldValue; // Property 'oldValue' does not exist on type 'DifferenceCreate'
			break;
		case "REMOVE":
			console.log(difference); // type-hint : DifferenceRemove
			difference.value; // Property 'value' does not exist on type 'DifferenceRemove'
			break;
		case "CHANGE":
			console.log(difference); // type-hint : DifferenceChange
			difference.value // OK
			difference.oldValue // OK
			break;
	}
}

@AsyncBanana
Copy link
Owner

Looks good! It should help the development experience with TypeScript. Thanks for the PR!

@AsyncBanana AsyncBanana merged commit c803311 into AsyncBanana:master Jul 27, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants