Skip to content

Commit

Permalink
Change DefaultTFuncReturn to return null if returnNull typeOpti…
Browse files Browse the repository at this point in the history
…on is true (#1865)

* Change DefaultTFuncReturn to return null if returnNull typeOption is true

* Fix tests
  • Loading branch information
pedrodurek committed Nov 12, 2022
1 parent 26073c4 commit 95489dd
Show file tree
Hide file tree
Showing 4 changed files with 9 additions and 8 deletions.
2 changes: 1 addition & 1 deletion index.d.ts
Expand Up @@ -860,7 +860,7 @@ type NormalizeMultiReturn<T, V> = V extends `${infer N}:${infer R}`
: never
: never;

export type DefaultTFuncReturn = string | undefined | null;
export type DefaultTFuncReturn = string | (TypeOptions['returnNull'] extends true ? null : never);

export type DefaultTFuncReturnWithObject = DefaultTFuncReturn | object | Array<string | object>;

Expand Down
1 change: 1 addition & 0 deletions test/typescript/custom-types/i18next.d.ts
Expand Up @@ -3,6 +3,7 @@ import 'i18next';
declare module 'i18next' {
interface CustomTypeOptions {
defaultNS: 'custom';
returnNull: false;
resources: {
custom: {
foo: 'foo';
Expand Down
7 changes: 7 additions & 0 deletions test/typescript/custom-types/t.test.ts
Expand Up @@ -45,3 +45,10 @@ function i18nextTPluralsUsage() {
i18next.t('plurals:foo', { count: 1 });
i18next.t('plurals:foo_many', { count: 10 });
}

// @ts-expect-error
function returnNullWithFalseValue(t: TFunction<string>) {
function fn(value: null) {}
// @ts-expect-error
fn(t('foo'));
}
7 changes: 0 additions & 7 deletions test/typescript/t.test.ts
Expand Up @@ -43,13 +43,6 @@ function callsMethodWithOptionalNullArg(t: TFunction) {
displayHint(t('friend'));
}

function callsMethodWithOptionalArg(t: TFunction) {
function displayHint(hint?: string) {
return String(hint);
}
displayHint(t('friend'));
}

function callsMethodWithRequiredNullArg(t: TFunction) {
function displayHint(hint: string | null) {
return String(hint);
Expand Down

0 comments on commit 95489dd

Please sign in to comment.