简体   繁体   English

TypeScript 扩展参数类型

[英]TypeScript extend parameter type

I want to call a function from my UI library with a type which extends the original ( Suggestion ) parameter type with additional properties.我想从我的 UI 库中调用一个 function,其类型扩展了具有附加属性的原始 ( Suggestion ) 参数类型。 According to this link it looks like it should be possible: https://github.com/Microsoft/TypeScript/issues/2225 (bottom comments)根据这个链接,它看起来应该是可能的: https://github.com/Microsoft/TypeScript/issues/2225 (底部评论)

What am I doing wrong and do you do this correctly?我做错了什么,你做对了吗? Note that the getSuggestion function and its type is located in a UI library package and I want to call it from my apps package.请注意, getSuggestion function 及其类型位于 UI 库 package 中,我想从我的应用程序 package 中调用它。

Playground link 游乐场链接

Code:代码:

import React from 'react'

type Suggestion = {
    name: string
}

interface UIProps {
    getSuggestion: <T = {}>(suggestion: Suggestion & T) => string
}

function UIComponent(props: UIProps) {
    return null
}

type AppSuggestion = {
  name: string
  enabled: boolean
}

function AppComponent() {
    return (
        <UIComponent
          getSuggestion={(suggestion: AppSuggestion) => 'hello from app'}
        />
    )
}

Error:错误: 类型“(suggestion: AppSuggestion) => string”不可分配给类型“<T = {}>(suggestion: Suggestion & T) => string”。参数类型“建议”和“建议”不兼容。 “建议”类型中缺少属性“已启用”,但在“AppSuggestion”类型中是必需的。(2322)

I have also tried using interfaces instead, but without luck.我也尝试过使用接口,但没有成功。

Playground link 游乐场链接

Code:代码:

import React from 'react'

interface Suggestion {
    name: string
}

interface UIProps {
    getSuggestion: <T>(suggestion: T) => string
}

function UIComponent(props: UIProps) {
    return null
}

interface AppSuggestion extends Suggestion {
  name: string
  enabled: boolean
}

function AppComponent() {
    return (
        <UIComponent
          getSuggestion={(suggestion: AppSuggestion) => 'hello from app'}
        />
    )
}

Error:错误: 类型“(suggestion: AppSuggestion) => string”不可分配给类型“(suggestion: T) => string”。参数类型“建议”和“建议”不兼容。类型“T”不可分配给类型“AppSuggestion”。(2322)

The generic type parameter should be on the interface and the component, not the function. If you have a generic function the implementation ( (suggestion: AppSuggestion) => 'hello from app' ) needs to accept any type argument for the type parameter, which it currently does not.通用类型参数应该在接口和组件上,而不是 function。如果你有一个通用的 function 实现( (suggestion: AppSuggestion) => 'hello from app' )需要接受类型参数的任何类型参数,目前没有。

You need to put the type parameter on the component and the props.您需要将类型参数放在组件和道具上。 This means that the type parameter will be inferred when you create your component:这意味着类型参数将在您创建组件时被推断出来:

interface UIProps<T> {
    getSuggestion: (suggestion: T) => string
}

function UIComponent<T>(props: UIProps<T>) {
    return null
}

interface AppSuggestion extends Suggestion {
  name: string
  enabled: boolean
}

function AppComponent() {
    return (
        <UIComponent
          getSuggestion={(suggestion: AppSuggestion) => 'hello from app'}
        />
    )
}

Playground Link 游乐场链接

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM