-
-
Notifications
You must be signed in to change notification settings - Fork 1k
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
feat(types): add returnNull and returnEptyString options to TypeOptions interface #1341
feat(types): add returnNull and returnEptyString options to TypeOptions interface #1341
Conversation
ts4.1/index.d.ts
Outdated
type NonNullableTranslation< | ||
T, | ||
U extends Partial<CustomTypeParameters> | ||
> = U['returnNull'] extends false ? (T extends null ? string : T) : T; | ||
|
||
type NonEmptyStringTranslation< | ||
T, | ||
U extends Partial<CustomTypeParameters> | ||
> = U['returnEmptyString'] extends false ? (T extends '' ? string : T) : T; | ||
|
||
/** | ||
* Checks if user has enabled `returnEmptyString` and `returnNull` options to retrieve correct values. | ||
*/ | ||
export type NormalizeByTypeOptions< | ||
T, | ||
U extends Partial<CustomTypeParameters> = TypeOptions | ||
> = NonNullableTranslation<NonEmptyStringTranslation<T, U>, U>; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
As both helpers are pretty much the same, you could make it more reusable, something like:
type NonNullableTranslation< | |
T, | |
U extends Partial<CustomTypeParameters> | |
> = U['returnNull'] extends false ? (T extends null ? string : T) : T; | |
type NonEmptyStringTranslation< | |
T, | |
U extends Partial<CustomTypeParameters> | |
> = U['returnEmptyString'] extends false ? (T extends '' ? string : T) : T; | |
/** | |
* Checks if user has enabled `returnEmptyString` and `returnNull` options to retrieve correct values. | |
*/ | |
export type NormalizeByTypeOptions< | |
T, | |
U extends Partial<CustomTypeParameters> = TypeOptions | |
> = NonNullableTranslation<NonEmptyStringTranslation<T, U>, U>; | |
type TypeOptionsFallback<T, U, R> = U extends false ? (T extends R ? string : T) : T | |
/** | |
* Checks if user has enabled `returnEmptyString` and `returnNull` options to retrieve correct values. | |
*/ | |
export type NormalizeByTypeOptions< | |
T, | |
U extends Partial<CustomTypeParameters> = TypeOptions, | |
R = TypeOptionsFallback<T, U['returnEmptyString'], ''> | |
> = TypeOptionsFallback<R, U['returnNull'], null>; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yup! I applied your suggestion, but changing the generic parameters naming. What do you think ? @pedrodurek
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Sounds good to me!
ts4.1/index.d.ts
Outdated
type CustomTypeParameters = { | ||
returnNull: boolean; | ||
returnEmptyString: boolean; | ||
}; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Can't we simply rely on type CustomTypeParameters = Pick<TypeOptions, 'returnNull' | 'returnEmptyString'>;
?
In case we can't, we can make both parameters optional instead of using partial in three different places:
type CustomTypeParameters = {
returnNull?: boolean;
returnEmptyString?: boolean;
};
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
We can't make that Pick, because the types for returnNull and returnEmptyString would be true
, not boolean :(
Applied your suggestion about making the keys optional and remove that Partial @pedrodurek
const emptyStringValue2: ReturnEmptyString = ''; | ||
|
||
// @ts-expect-error: '"non-empty-string"' is not assignable to type '""' | ||
const nonEmptyStringValue2: ReturnEmptyString = 'non-empty-string'; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I'm always in favour of testing everything integrated by testing both flags in the useTranslation
, but I'm afraid it's not possible to toggle them on/off 🤔
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yep, I don't know if there's a way to make the augmentation twice to check both flags. If you have any idea, just let me know :) @pedrodurek
…ns interface - Add utility types in order to return correct types for both returnEmtpyString and returnNull options - Add test cases for both options
d01a300
to
228deb7
Compare
is this ready to merge? |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
LGTM! 🎉
published in react-i18next@11.11.4 |
Working with react-i18next and typescript, I found that returnNull and returnEptyString options were not represented in typescript signatures.
By providing keys for these values inside the
CustomTypeOptions
, we can refine the return types for TFunction.Notice that these keys must be aligned with the actual options in the i18next instance in order to represent the actual behaviour of the library on runtime.
Checklist
npm run test