参数不能赋值为T类型的参数。

时间:2022-04-25 16:31:07

I have defined the interface IDictionary as follows:

我已经定义了接口IDictionary如下:

interface IDictionary<T = any> {
  [key: string]: T;
}

And then later I pass this type T into the db.update() function which is defined as:

然后,我将这个类型T传递给db.update()函数,该函数定义为:

public update<T = IDictionary>(path: string, value: T) {
  return this.ref(path).update(value);
}

When I try to compile this file, I get the error that:

当我试图编译这个文件时,我得到的错误是:

src/shared/Model.ts (64,7): Argument of type '{ lastUpdated: string; }' is not assignable to parameter of type 'T'. (2345)

src /共享/模型。ts(64,7):类型为{lastUpdated: string;}“不能指定类型为T的参数。(2345)

Considering the IDictionary<any> type is the default "T", I can't see why this wouldn't be considered valid. Is there something I'm doing wrong?

考虑到IDictionary 类型是默认的“T”,我不明白为什么它不能被认为是有效的。有什么我做错了吗?

参数不能赋值为T类型的参数。

complete example that demonstrates the error:

演示错误的完整示例:

interface IDictionary<T = any> {
  [key: string]: T;
}

function dbupdate<T = IDictionary>(path: string, value: T) {
  return this.ref(path).update(value);
}

abstract class Model<T = IDictionary> {

    public update(updateWith: IDictionary) {

        const reference = 'bar';
        return dbupdate<T>(
            reference,
            {
                ...updateWith,
                ...{ lastUpdated: 'baz' }
            }
        );
    }
}

1 个解决方案

#1


1  

In your Model class declaration

在模型类声明中。

abstract class Model<T = IDictionary> { 

T is generic type parameter that can be anything. IDictionary is the default value, it does not imply that T will always be compatible with IDictionary. Nothing prevents someone from using your Model like this:

T是可以是任何东西的泛型类型参数。IDictionary是默认值,它并不意味着T始终与IDictionary兼容。没有什么能阻止某人使用你的模型:

let myModel: Model<string> = ...

Typescript is telling you that in general case, the value you constructed for the second argument of db.update is not compatible with T.

Typescript告诉您,一般情况下,为db的第二个参数构建的值。更新与T不兼容。

The simplest way to make it compile is to change db.update call like this

使其编译的最简单方法是更改db。这样的更新的电话

    return dbupdate<IDictionary>(
        reference,
        {
            ...updateWith,
            ...{ lastUpdated: 'baz' }
        }
    );

However I have no idea what you are trying to achieve by having generic parameter T, so probably this is not the correct solution.

但是,我不知道您试图通过具有通用参数T来实现什么,所以这可能不是正确的解决方案。

#1


1  

In your Model class declaration

在模型类声明中。

abstract class Model<T = IDictionary> { 

T is generic type parameter that can be anything. IDictionary is the default value, it does not imply that T will always be compatible with IDictionary. Nothing prevents someone from using your Model like this:

T是可以是任何东西的泛型类型参数。IDictionary是默认值,它并不意味着T始终与IDictionary兼容。没有什么能阻止某人使用你的模型:

let myModel: Model<string> = ...

Typescript is telling you that in general case, the value you constructed for the second argument of db.update is not compatible with T.

Typescript告诉您,一般情况下,为db的第二个参数构建的值。更新与T不兼容。

The simplest way to make it compile is to change db.update call like this

使其编译的最简单方法是更改db。这样的更新的电话

    return dbupdate<IDictionary>(
        reference,
        {
            ...updateWith,
            ...{ lastUpdated: 'baz' }
        }
    );

However I have no idea what you are trying to achieve by having generic parameter T, so probably this is not the correct solution.

但是,我不知道您试图通过具有通用参数T来实现什么,所以这可能不是正确的解决方案。