简体   繁体   English

Typescript:具有 function 参数值的可区分联合解析为“任意”

[英]Typescript: Discriminated Union with value in function parameter resolves to 'any'

Both of these examples fail to see the type of the parameter in onSelect ... TS claims value is any if clearable is not provided.这两个示例都看不到onSelect中的参数类型...如果未提供clearable ,TS 声明valueany

I'm at a loss.我不知所措。 I've attempted both with and without a generic: no dice.我尝试过使用和不使用泛型:没有骰子。 Any help?有什么帮助吗?

import React from 'react';

interface RequiredProps {
    value: string;
    clearable?: false;
    onSelect: (value: string) => void;
}

interface ClearableProps {
    value: string | null;
    clearable?: true;
    onSelect: (value: string | null) => void;
}

const Select: React.FC<ClearableProps | RequiredProps> = ({ value, clearable, onSelect }) => {
    return <div />;
}

const Clearable = <Select value="abc" onSelect={(value) => { }} clearable={true} />;
const ExplicitNotClearable = <Select value="abc" onSelect={(value) => { }} clearable={false} />;

// Why doesn't TS know `value` is a `string`, but rather thinks this function has no type?
const NotClearable = <Select value="abc" onSelect={(value) => { }} />;
import React from 'react';

interface Props<T> {
    clearable?: boolean;
    onSelect: (value: T) => void;
    value: T;
}

interface RequiredProps extends Props<string> {
    clearable?: false;
}

type ClearableType = string | null;
interface ClearableProps extends Props<ClearableType> {
    clearable: true;
}

const Select: React.FC<ClearableProps | RequiredProps> = ({ value, clearable, onSelect }) => {
    return <div />;
}

const Clearable = <Select value="abc" onSelect={(value) => { }} clearable={true} />;
const ExplicitNotClearable = <Select value="abc" onSelect={(value) => { }} clearable={false} />;

// Why doesn't TS know `value` is a `string`, but rather thinks this function has no type?
const NotClearable = <Select value="abc" onSelect={(value) => { }} />;

I'm a little confused because in your two cases, clearable is either optional or required in ClearableProps .我有点困惑,因为在您的两种情况下, clearableClearableProps中是可选的或必需的。 I expect you mean it to be required in both versions, right?我希望您的意思是在两个版本中都需要它,对吗? Anyway, I will assume that.无论如何,我会假设。

This looks like a known bug, microsoft/TypeScript#31618 .这看起来像一个已知的错误, microsoft/TypeScript#31618 The value callback parameter is not being contextually typed when the callback is assigned to a union-typed onSelect property.当回调分配给联合类型的onSelect属性时, value回调参数不是上下文类型的。 When you explicitly discriminate the union by setting clearable the problem goes away.当您通过设置clearable明确区分联合时,问题就消失了。

This issue is on the backlog, so it's not likely to be addressed anytime soon.此问题已积压,因此不太可能很快得到解决。

You can work around this by explicity annotating the type of value :您可以通过显式注释value的类型来解决此问题:

const ImplicitlyNotClearable = <Select value="abc" onSelect={(value: string) => { }} />;

which isn't the best, but it least it's something.这不是最好的,但至少它是一些东西。

Okay, hope that helps;好的,希望有帮助; good luck!祝你好运!

Playground link to code Playground 代码链接

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 打字稿区分联合与交集 - Typescript discriminated union with intersection TypeScript 带可选判别式的判别联合 - TypeScript Discriminated Union with Optional Discriminant 有什么方法可以添加鉴别器属性以使用 Axios + Typescript 将数据读入鉴别联合对象中吗? - Is there any way to add a discriminator property to read data into a discriminated union object with Axios + Typescript? TypeScript 中的条件泛型类型使用 Discriminated Union - Conditional Generic Types in TypeScript use Discriminated Union 对于具有扩展的可区分联合的 React 组件,TypeScript 错误“不可分配给类型‘IntrinsicAttributes’” - TypeScript error "not assignable to type 'IntrinsicAttributes'" for React component with extended discriminated union TypeScript 区分联合不会为不存在的字段提供错误 - TypeScript discriminated union does not give error for non-existing fields React Typescript 可区分联合给出编译错误,但适用于 IDE - React Typescript discriminated union gives compiling error but works on IDE Typescript 接口 - 使用窄联合类型参数实现 function - Typescript Interfaces - Implement function with narrowed union type parameter 记录和受歧视的工会访问 - Records and discriminated union access 使用 TypeScript,您如何允许未定义的道具但仍然强制执行受歧视的联合? - Using TypeScript, how can you allow undefined props but still enforce a discriminated union?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM