函数 return 类型参数依赖 Typescript
Function return type parameter dependent Typescript
我正在尝试制作一个将字符串转换为布尔值的函数,并带有可选的回退以防函数失败。我尝试了两种简单的方法,但我无法理解为什么它不起作用。
方法一:
使用条件类型。
type toBooleanReturnType<T> =
T extends undefined
? boolean | undefined
: boolean | T
/**
* Converts a string to a boolean
* @param str
* @param fallback the fallback value in case the string param is not 'true' nor 'false
* @returns
*/
export const toBoolean = <T = undefined>(
str: string,
fallback?: T,
): toBooleanReturnType<T> => {
if (str.trim() === 'true') return true;
if (str.trim() === 'false') return false;
if (fallback) return fallback;
return undefined;
};
我得到 Type 'T' is not assignable to type 'toBooleanReturnType<T>'
fallback
return 和
Type 'undefined' is not assignable to type 'toBooleanReturnType<T>'
为 undefined
return.
方法二:
函数重载。
/**
* Converts a string to a boolean
* @param str
* @param fallback the fallback value in case the string param is not 'true' nor 'false
* @returns
*/
export function toBoolean<T>(
str: string,
fallback?: undefined,
): boolean | undefined
export function toBoolean<T>(
str: string,
fallback: T,
): boolean | T {
if (str.trim() === 'true') return true;
if (str.trim() === 'false') return false;
if (fallback) return fallback as T;
return undefined;
};
但我也收到 undefined
return 的错误。 Type 'undefined' is not assignable to type 'boolean | T'.
这样做的正确方法是什么?
糟糕,抱歉。在我发布问题的那一刻就解决了这个问题。
/**
* Converts a string to a boolean
* @param str
* @param fallback the fallback value in case the string param is not 'true' nor 'false
* @returns
*/
export const toBoolean = <T = undefined>(
str: string,
fallback?: T,
): T | boolean => {
if (str.trim() === 'true') return true;
if (str.trim() === 'false') return false;
return fallback as T;
};
实际上,它在你的后备 return 中,你可以 return 任何东西,因为你可以在后备中传递任何你想要的东西。
这将是一个答案:
function toBoolean(
str: string,
fallback?: any,
): boolean | any | undefined
{
return str.trim() === 'true' ? true : str.trim() === 'false' ? false : fallback? fallback : undefined;
};
或者如果你想要 undefined 你可以做什么:
function toBoolean(
str: string,
): boolean | undefined
{
return str.trim() === 'true' ? true : str.trim() === 'false' ? false : undefined;
};
你不需要传递另一个参数作为回退,就好像它不是真或假一样,它会 return undefined anyway
我正在尝试制作一个将字符串转换为布尔值的函数,并带有可选的回退以防函数失败。我尝试了两种简单的方法,但我无法理解为什么它不起作用。
方法一: 使用条件类型。
type toBooleanReturnType<T> =
T extends undefined
? boolean | undefined
: boolean | T
/**
* Converts a string to a boolean
* @param str
* @param fallback the fallback value in case the string param is not 'true' nor 'false
* @returns
*/
export const toBoolean = <T = undefined>(
str: string,
fallback?: T,
): toBooleanReturnType<T> => {
if (str.trim() === 'true') return true;
if (str.trim() === 'false') return false;
if (fallback) return fallback;
return undefined;
};
我得到 Type 'T' is not assignable to type 'toBooleanReturnType<T>'
fallback
return 和
Type 'undefined' is not assignable to type 'toBooleanReturnType<T>'
为 undefined
return.
方法二: 函数重载。
/**
* Converts a string to a boolean
* @param str
* @param fallback the fallback value in case the string param is not 'true' nor 'false
* @returns
*/
export function toBoolean<T>(
str: string,
fallback?: undefined,
): boolean | undefined
export function toBoolean<T>(
str: string,
fallback: T,
): boolean | T {
if (str.trim() === 'true') return true;
if (str.trim() === 'false') return false;
if (fallback) return fallback as T;
return undefined;
};
但我也收到 undefined
return 的错误。 Type 'undefined' is not assignable to type 'boolean | T'.
这样做的正确方法是什么?
糟糕,抱歉。在我发布问题的那一刻就解决了这个问题。
/**
* Converts a string to a boolean
* @param str
* @param fallback the fallback value in case the string param is not 'true' nor 'false
* @returns
*/
export const toBoolean = <T = undefined>(
str: string,
fallback?: T,
): T | boolean => {
if (str.trim() === 'true') return true;
if (str.trim() === 'false') return false;
return fallback as T;
};
实际上,它在你的后备 return 中,你可以 return 任何东西,因为你可以在后备中传递任何你想要的东西。
这将是一个答案:
function toBoolean(
str: string,
fallback?: any,
): boolean | any | undefined
{
return str.trim() === 'true' ? true : str.trim() === 'false' ? false : fallback? fallback : undefined;
};
或者如果你想要 undefined 你可以做什么:
function toBoolean(
str: string,
): boolean | undefined
{
return str.trim() === 'true' ? true : str.trim() === 'false' ? false : undefined;
};
你不需要传递另一个参数作为回退,就好像它不是真或假一样,它会 return undefined anyway