[英]Can a dynamic type function param be extended?
Is this a possibility (and if so how)?这有可能吗(如果有的话如何)?
type extendedValues = { name: string };
type Props<T> {
values: T & extendedValues ?
}
const ReactFuncComponent = <T extends Props<T>>({ values }: Props<T>) => {
...
}
So essentially I want to say you can pass in an object that contains any fields with any values but it must also include a name of type string:所以基本上我想说你可以传入一个 object,它包含具有任何值的任何字段,但它还必须包含字符串类型的名称:
<ReactFuncComponent values={{ age: 0, name: '' }} />
If I understood your question correctly, you can achieve what you want in following way:如果我正确理解你的问题,你可以通过以下方式实现你想要的:
interface Credentials { name: string; } type ExtendedValues<T> = Credentials & T; type Props<T> = { value: ExtendedValues<T> } const TestComponent = <T, >(props: Props<T>) => { return <p>Some text here</p> }; // Example of usage <TestComponent value={{name: "Name"}} /> // Will be OK <TestComponent value={{name: "Name", age: 25}} /> // Will be OK <TestComponent value={{age: 25}} /> // Error: property 'name' is missing
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.