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

Fix the .get() TypeScript return type #117

Merged
merged 10 commits into from
Aug 13, 2020
Merged

Fix the .get() TypeScript return type #117

merged 10 commits into from
Aug 13, 2020

Conversation

SnosMe
Copy link
Contributor

@SnosMe SnosMe commented Jul 26, 2020

Closes #116

source/index.ts Outdated Show resolved Hide resolved
source/index.ts Outdated Show resolved Hide resolved
@SnosMe SnosMe requested a review from sindresorhus July 27, 2020 11:22
test/index.test-d.ts Outdated Show resolved Hide resolved
source/index.ts Outdated Show resolved Hide resolved
@sindresorhus sindresorhus changed the title fix: "get" function type Fix the .get() TypeScript return type Jul 29, 2020
expectType<string | undefined>(conf.get('foo'));
expectType<string>(conf.get('foo'));
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The second one was indeed incorrect. But Is it correct to assume that a value will never be undefined when we do not pass a default? I think we should still make it possible to be undefined to encourage people to handle an undefined state.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes, value will never be undefined unless developer make a bug such in this test, type UnicornFoo is source of truth.

type UnicornFoo = {
	foo: string;
};
const conf = new Conf<UnicornFoo>({accessPropertiesByDotNotation: true});

Maybe we should validate default option, but it is out of scope of this PR.
Like this

	defaults?: Readonly<{
		[P in keyof T]: Exclude<T[P], undefined>
	}>;

Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Maybe we should validate default option, but it is out of scope of this PR.

That would be nice, yes, but I agree, it should be a separate PR.

@sindresorhus sindresorhus merged commit 526002e into sindresorhus:master Aug 13, 2020
@sindresorhus
Copy link
Owner

Thanks for fixing this, @SnosMe 🙌🏻

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.

TypeScript types should take into account defaults
3 participants