TypeScript 条件类型和类型保护
TypeScript Conditional Types and Type Guards
我在结合 TypeScript 的类型保护和条件类型时遇到了一些问题。考虑:
export interface IThisThing {
someProp: number;
}
export function isIThisThing(type: any): type is IThisThing {
return !!type.someProp;
}
export interface IThatThing {
someOtherProp: string;
}
export function isIThatThing(type: any): type is IThatThing {
return !!type.someOtherProp;
}
function doAThing<T extends IThisThing | IThatThing>(
data: T
): T extends IThisThing ? IThisThing : IThatThing {
if (isIThisThing(data)) {
return data; // Type 'T & IThisThing' is not assignable to type 'T extends IThisThing ? IThisThing : IThatThing'.
};
return data; // Type 'T' is not assignable to type 'T extends IThisThing ? IThisThing : IThatThing'.
// Type 'IThisThing | IThatThing' is not assignable to type 'T extends IThisThing ? IThisThing : IThatThing'.
// Type 'IThisThing' is not assignable to type 'T extends IThisThing ? IThisThing : IThatThing'.
}
我希望 doAThing
函数接受 IThisThing
或 IThatThing
并接受 return 与其接收的类型相同的类型。唉,编译器被以下行的消息阻塞了:
Type 'T & IThisThing' is not assignable to type 'T extends IThisThing ? IThisThing : IThatThing'.
谁能帮我弄清楚?我觉得我很接近但不太正确。我在这个博客 post: http://artsy.github.io/blog/2018/11/21/conditional-types-in-typescript/
中使用第一个例子(看起来很相似)
Typescript 不允许您将任何内容分配给仍然具有自由类型参数的条件类型,只是不支持它。你最好的选择是拥有一个带有泛型和条件类型的签名以及一个更简单的实现签名,returns 两种可能性的结合
export interface IThisThing {
someProp: number;
}
export function isIThisThing(type: any): type is IThisThing {
return !!type.someProp;
}
export interface IThatThing {
someOtherProp: string;
}
export function isIThatThing(type: any): type is IThatThing {
return !!type.someOtherProp;
}
function doAThing<T extends IThisThing | IThatThing>(
data: T
): T extends IThisThing ? IThisThing : IThatThing
function doAThing(
data: IThisThing | IThatThing
): IThisThing | IThatThing {
if (isIThisThing(data)) {
return data;
};
return data;
}
就return T
:
function doAThing<T extends IThisThing | IThatThing>(
data: T
): T {
if (isIThisThing(data)) {
return data
} else {
return data;
}
}
我在结合 TypeScript 的类型保护和条件类型时遇到了一些问题。考虑:
export interface IThisThing {
someProp: number;
}
export function isIThisThing(type: any): type is IThisThing {
return !!type.someProp;
}
export interface IThatThing {
someOtherProp: string;
}
export function isIThatThing(type: any): type is IThatThing {
return !!type.someOtherProp;
}
function doAThing<T extends IThisThing | IThatThing>(
data: T
): T extends IThisThing ? IThisThing : IThatThing {
if (isIThisThing(data)) {
return data; // Type 'T & IThisThing' is not assignable to type 'T extends IThisThing ? IThisThing : IThatThing'.
};
return data; // Type 'T' is not assignable to type 'T extends IThisThing ? IThisThing : IThatThing'.
// Type 'IThisThing | IThatThing' is not assignable to type 'T extends IThisThing ? IThisThing : IThatThing'.
// Type 'IThisThing' is not assignable to type 'T extends IThisThing ? IThisThing : IThatThing'.
}
我希望 doAThing
函数接受 IThisThing
或 IThatThing
并接受 return 与其接收的类型相同的类型。唉,编译器被以下行的消息阻塞了:
Type 'T & IThisThing' is not assignable to type 'T extends IThisThing ? IThisThing : IThatThing'.
谁能帮我弄清楚?我觉得我很接近但不太正确。我在这个博客 post: http://artsy.github.io/blog/2018/11/21/conditional-types-in-typescript/
中使用第一个例子(看起来很相似)Typescript 不允许您将任何内容分配给仍然具有自由类型参数的条件类型,只是不支持它。你最好的选择是拥有一个带有泛型和条件类型的签名以及一个更简单的实现签名,returns 两种可能性的结合
export interface IThisThing {
someProp: number;
}
export function isIThisThing(type: any): type is IThisThing {
return !!type.someProp;
}
export interface IThatThing {
someOtherProp: string;
}
export function isIThatThing(type: any): type is IThatThing {
return !!type.someOtherProp;
}
function doAThing<T extends IThisThing | IThatThing>(
data: T
): T extends IThisThing ? IThisThing : IThatThing
function doAThing(
data: IThisThing | IThatThing
): IThisThing | IThatThing {
if (isIThisThing(data)) {
return data;
};
return data;
}
就return T
:
function doAThing<T extends IThisThing | IThatThing>(
data: T
): T {
if (isIThisThing(data)) {
return data
} else {
return data;
}
}