打字稿:具有原始类型约束的泛型类型 [英] typescript : generic type with primitive types constrain

查看:35
本文介绍了打字稿:具有原始类型约束的泛型类型的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在打字稿中有以下泛型类

I have the following generic classes in typescript

type UserId = number
type Primitive = string | number | boolean
class ColumnValue<T, S extends Primitive> {
    constructor(public columnName: String, public value: S) { }
}
abstract class Column<T> {
    constructor(public columnName: String) { }
    public set<S extends Primitive>(value: T): ColumnValue<T, S> {
        return new ColumnValue(this.columnName, this.getValue(value))
    }
    public abstract getValue<S extends Primitive>(value: T): S
}
let id = new class extends Column<UserId> {
    constructor() { super("id") }
    public getValue(value: UserId): number {
        return value
    }
}()

但我不知道为什么会出现此错误 Class '(Anonymous class)' 错误地扩展了基类 'Column'.属性getValue"的类型不兼容.类型 '(value: number) => number' 不能分配给类型 '(value: number) => S'.数字"类型不能分配给S"类型

but I don't know why get this error Class '(Anonymous class)' incorrectly extends base class 'Column'. Types of property 'getValue' are incompatible. Type '(value: number) => number' is not assignable to type '(value: number) => S'. Type 'number' is not assignable to type 'S'

推荐答案

On Column getter 和 setter S 不一定是相同的类型,所以你应该移动类型其父类的参数:Column.

On Column getter and setter S isn't necessarily the same type so you should move the type param to its parent class: Column<T, S extends Primitive>.

type UserId = number
type Primitive = string | number | boolean
class ColumnValue<T, S extends Primitive> {
    constructor(public columnName: String, public value: S) { }
}
abstract class Column<T, S extends Primitive> {
    constructor(public columnName: String) { }
    public set(value: T): ColumnValue<T, S> {
        return new ColumnValue(this.columnName, this.getValue(value))
    }
    public abstract getValue(value: T): S
}
let id = new class extends Column<UserId, number> {
    constructor() { super("id") }
    public getValue(value: UserId): number {
        return value
    }
}()

以上版本至少没有错误.

我知道您可能想从与 setter 一起使用的任何类型推断 S,但 Column 在实例化时必须具有明确定义的类型,因此这意味着您要么在调用构造函数时显式(即 new Column(...)),要么在构造函数中添加一个 S 参数,以便 S 可以从中推断出来(如 new Column('id', 123))

I understand that you probably want to infer the S from whatever type you use with you setter but Column must have a well defined type upon instantiation, so that means that you either explicit when calling the constructor (that is new Column<UserId, number>(...)) or add a S param in the constructor so S can be inferred from it (like in new Column<UserId>('id', 123))

这篇关于打字稿:具有原始类型约束的泛型类型的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆