带有Typescript的变量类型保护
我正在尝试创建一个更高阶的函数,它允许我创建一个通过类型保护来确认类型的函数带有Typescript的变量类型保护,typescript,Typescript,我正在尝试创建一个更高阶的函数,它允许我创建一个通过类型保护来确认类型的函数 type Shape = Circle | Triangle | Rectangle enum Type { Circle, Triangle, Rectangle } interface Circle { type: Type.Circle; diameter: number; } interface Triangle { type: Type.Triangle; width: number; }
type Shape = Circle | Triangle | Rectangle
enum Type { Circle, Triangle, Rectangle }
interface Circle {
type: Type.Circle;
diameter: number;
}
interface Triangle {
type: Type.Triangle;
width: number;
}
interface Rectangle {
type: Type.Rectangle;
width: number;
height: number;
}
const isShape = (condition: Type) => (shape: Shape): shape is ? => shape.type == condition;
const isCircle = isShape(Type.Circle);
在上面的示例中,我希望
isCircle
函数返回类型是否为Circle
。?
是一个占位符,因为我无法让它工作。为了实现您的目标,以下是isShape函数的外观:
const isShape = <T extends Shape>(condition: Type) => {
return function(shape: T): shape is T {
return shape.type == condition;
};
};
const isCircle = isShape<Circle>(Type.Circle);
常量isShape=(条件:类型)=>{
返回函数(形状:T):形状为T{
返回shape.type==条件;
};
};
常量isCircle=isShape(Type.Circle);
为了实现您的目标,以下是isShape函数的外观:
const isShape = <T extends Shape>(condition: Type) => {
return function(shape: T): shape is T {
return shape.type == condition;
};
};
const isCircle = isShape<Circle>(Type.Circle);
常量isShape=(条件:类型)=>{
返回函数(形状:T):形状为T{
返回shape.type==条件;
};
};
常量isCircle=isShape(Type.Circle);
您需要为TypeScript提供一种直接的方法,以确定与给定的类型相关联的形状。一种方法是显式地提供两者之间的映射,然后可以使用该映射替换形状的定义。例如:
// Instead of this:
// type Shape = Circle | Triangle | Rectangle
// Use this:
type TypeToShape = {
[Type.Circle]: Circle,
[Type.Triangle]: Triangle,
[Type.Rectangle]: Rectangle,
}
// Circle | Triangle | Rectangle
type Shape = TypeToShape[keyof TypeToShape];
然后,给定特定的类型
,很容易将其映射到与其关联的形状
:
// Use the type map to tell typescript what shape goes with the Type K:
const isShape = <K extends Type>(condition: K) => (shape: Shape): shape is TypeToShapeMap[K] => shape.type == condition;
const isCircle = isShape(Type.Circle); // return type: shape is Circle
const isTriangle = isShape(Type.Triangle) // return type: shape is Triangle
//使用类型映射告诉typescript什么形状与类型K匹配:
const isShape=(条件:K)=>(形状:形状):形状是TypeToShapeMap[K]=>shape.type==条件;
常量isCircle=isShape(Type.Circle);//返回类型:形状为圆形
const isTriangle=isShape(Type.Triangle)//返回类型:形状是三角形
您需要为TypeScript提供一种直接的方法,以确定与给定的类型相关联的形状。一种方法是显式地提供两者之间的映射,然后可以使用该映射替换形状的定义。例如:
// Instead of this:
// type Shape = Circle | Triangle | Rectangle
// Use this:
type TypeToShape = {
[Type.Circle]: Circle,
[Type.Triangle]: Triangle,
[Type.Rectangle]: Rectangle,
}
// Circle | Triangle | Rectangle
type Shape = TypeToShape[keyof TypeToShape];
然后,给定特定的类型
,很容易将其映射到与其关联的形状
:
// Use the type map to tell typescript what shape goes with the Type K:
const isShape = <K extends Type>(condition: K) => (shape: Shape): shape is TypeToShapeMap[K] => shape.type == condition;
const isCircle = isShape(Type.Circle); // return type: shape is Circle
const isTriangle = isShape(Type.Triangle) // return type: shape is Triangle
//使用类型映射告诉typescript什么形状与类型K匹配:
const isShape=(条件:K)=>(形状:形状):形状是TypeToShapeMap[K]=>shape.type==条件;
常量isCircle=isShape(Type.Circle);//返回类型:形状为圆形
const isTriangle=isShape(Type.Triangle)//返回类型:形状是三角形
相关。简言之,如果不重新构造您的类型,就不可能为满足您需要的isShape
提供签名。@CRice,要实现这一点,必须如何构造类型?相关。简而言之,如果不重新构造您的类型,就不可能为符合您要求的isShape
提供签名。@CRice,这些类型必须如何构造才能实现这一点?这是错误的:它没有在条件
和t
@Akshar Patel之间建立关系,不幸的是,当这个函数被实现时,Typescript只会告诉你类型是Shape
@LennardSchutter:现在呢?@Akshar Patel,这很有效,太棒了!我没有意识到我可以将
传递给函数调用方。@Akshar Patel,我最终使它更通用了一点<代码>常量isType=()=>(条件:E)=>(obj:U):obj是T=>obj.type==条件
这是错误的:它没有在条件
和T
@Akshar Patel之间建立关系,不幸的是,当这个函数实现时,Typescript只会告诉你类型是形状
@LennardSchutter:现在怎么样?@Akshar Patel,这很有效,太棒了!我没有意识到我可以将
传递给函数调用方。@Akshar Patel,我最终使它更通用了一点<代码>常量isType=()=>(条件:E)=>(obj:U):obj是T=>obj.type==条件谢谢你的努力,@CRice。虽然这也可以,但我发现另一个答案更容易实现,并且不需要更改我当前的代码。@CRice:这是一个非常干净的解决方案。谢谢你的努力,@CRice。虽然这也可以,但我发现另一个答案更容易实现,并且不需要更改我当前的代码。@CRice:这是一个非常干净的解决方案。