英文:
Extending a type to allow additional fields
问题
我有一个通用的方法,用于为数据库创建节点,我希望这个方法能够允许使用类型中已知的字段以及任何额外的字段。
以下是相关的方法代码:
create<A extends string, P extends Partial<Record<keyof T['fields'], any>>>(
alias: A,
node: T,
parameters: P,
): this {
const properties = Object.entries(parameters)
.map(([key, value]) => `${key}: "${value}"`)
.join(', ');
this.query.push(`CREATE (${alias}:${node.label} {${properties}})`);
return this;
}
当我使用这个方法时:
const query = this.cypherBuilder
.create('m', Author, {
name,
image,
description,
lifeYears,
didntExisit: 22, // 这个字段不是Author的字段之一。
})
.return('m')
.build();
TypeScript没有对didntExisit字段进行任何投诉,尽管它不是Author的字段之一。
英文:
I have a generic method designed to create nodes for a database, and I want this method to allow both known fields from a type and any additional fields.
Here's the method in question:
create<A extends string, P extends Partial<Record<keyof T['fields'], any>>>(
alias: A,
node: T,
parameters: P,
): this {
const properties = Object.entries(parameters)
.map(([key, value]) => `${key}: "${value}"`)
.join(', ');
this.query.push(`CREATE (${alias}:${node.label} {${properties}})`);
return this;
}
When I use this method:
const query = this.cypherBuilder
.create('m', Author, {
name,
image,
description,
lifeYears,
didntExisit: 22, // This field isn't part of Author's fields.
})
.return('m')
.build();
TypeScript didn't complains about the didntExisit field, which isn't part of Author's fields.
答案1
得分: 1
一个解决方法是使用映射类型来根据类型的已知字段限制参数对象的键。以下是如何实现这一点的示例代码:
type RestrictedParameters<T> = Partial<{ [K in keyof T['fields']]: any }>;
class CypherBuilder<T> {
// ... 其他方法 ...
create<A extends string>(
alias: A,
node: T,
parameters: RestrictedParameters<T>,
): this {
const properties = Object.entries(parameters)
.map(([key, value]) => `${key}: "${value}"`)
.join(', ');
this.query.push(`CREATE (${alias}:${node.label} {${properties}})`);
return this;
}
}
英文:
A workaround is to use a mapped type to restrict the keys of the parameters object based on the type's known fields. Here's how you can achieve this:
type RestrictedParameters<T> = Partial<{ [K in keyof T['fields']]: any }>;
class CypherBuilder<T> {
// ... other methods ...
create<A extends string>(
alias: A,
node: T,
parameters: RestrictedParameters<T>,
): this {
const properties = Object.entries(parameters)
.map(([key, value]) => `${key}: "${value}"`)
.join(', ');
this.query.push(`CREATE (${alias}:${node.label} {${properties}})`);
return this;
}
}
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论