有没有办法在TypeScript中定义setter函数?
我发现下面的getter函数非常有用且可重用,因为它可以获取具有此类属性的任何对象的属性键的值有没有办法在TypeScript中定义setter函数?,typescript,Typescript,我发现下面的getter函数非常有用且可重用,因为它可以获取具有此类属性的任何对象的属性键的值 export function keyOf<a>(value: { key: a; }) : a { return value.key; } 但这不是一段有效的TypeScript代码 问:我如何才能获得一个类型安全的通用setter函数,该函数返回原始对象及其属性集 更新: 在当前类型脚本中,禁止类型参数之间的依赖关系。我相信这样做是为了使打字系统更简单、更快。然而,这种限制阻止
export function keyOf<a>(value: { key: a; }) : a {
return value.key;
}
但这不是一段有效的TypeScript代码
问:我如何才能获得一个类型安全的通用setter函数,该函数返回原始对象及其属性集
更新:
在当前类型脚本中,禁止类型参数之间的依赖关系。我相信这样做是为了使打字系统更简单、更快。然而,这种限制阻止了某些有用的场景,比如所讨论的场景。有一种黑客可以将类型参数之间的依赖关系转化为函数依赖关系:
export function withKey<a, b>(
value: a,
key: b,
toAssignable: (value: a) => { key: b } = function<c>(anything: c) : c {
return anything;
}
) : a {
toAssignable(value).key = key;
return value;
}
这看起来像地狱一样丑陋,改变了原始签名,但它编译并工作
有谁知道更好的方法吗?我玩了一会儿,我只能想出这个:
// === unsave approach ===
/**
* Test class
*/
class MyClassWithoutKey {
public youCanCallThis() { }
}
/**
* The setter function
*/
function withKeyUnsafe<T>(value: T, key: any) {
(<any>value).key = key;
return value;
}
// compile test
var w = withKeyUnsafe(new MyClassWithoutKey(), 2).youCanCallThis();
这是:
// === typesave approach ===
/**
* Test class
*/
class MyClassWithKey {
key: string;
public youCanCallThis() { }
}
/**
* Helper interface
*/
interface WithKey<T> {
key: T;
}
/**
* Factory function that adds type constraint
*/
function withKeyTypeSave<T>() {
/**
* The actual setter function
*/
return function <WithKeyT extends WithKey<T>>(value: WithKeyT, key: T) {
value.key = key;
return value;
}
}
// compile test -- works
withKeyTypeSave<string>()(new MyClassWithKey(), "new key").youCanCallThis();
// compile test -- fails
withKeyTypeSave<number>()(new MyClassWithKey(), "new key").youCanCallThis();
// compile test -- fails
withKeyTypeSave<string>()(new MyClassWithKey(), 3).youCanCallThis();
您的通用setter函数看起来像是从不同语言导入的反模式。请参阅,但是如果您坚持泛型setter概念,那么请探索语言关键字接口和实现,例如,从何时泛型函数成为反模式?类太笨拙了,需要被实例化为对象,这会带来不必要的开销。我不想争论它是否是反模式,但是:如果不创建对象让它工作,你会如何使用你的方法呢?首先,它不是一个方法,它是一个函数。对于这样的函数,有很多有用的场景。在我的特殊情况下,我需要它在TypeScript中实现。
// === unsave approach ===
/**
* Test class
*/
class MyClassWithoutKey {
public youCanCallThis() { }
}
/**
* The setter function
*/
function withKeyUnsafe<T>(value: T, key: any) {
(<any>value).key = key;
return value;
}
// compile test
var w = withKeyUnsafe(new MyClassWithoutKey(), 2).youCanCallThis();
// === typesave approach ===
/**
* Test class
*/
class MyClassWithKey {
key: string;
public youCanCallThis() { }
}
/**
* Helper interface
*/
interface WithKey<T> {
key: T;
}
/**
* Factory function that adds type constraint
*/
function withKeyTypeSave<T>() {
/**
* The actual setter function
*/
return function <WithKeyT extends WithKey<T>>(value: WithKeyT, key: T) {
value.key = key;
return value;
}
}
// compile test -- works
withKeyTypeSave<string>()(new MyClassWithKey(), "new key").youCanCallThis();
// compile test -- fails
withKeyTypeSave<number>()(new MyClassWithKey(), "new key").youCanCallThis();
// compile test -- fails
withKeyTypeSave<string>()(new MyClassWithKey(), 3).youCanCallThis();