Welcome to MLink Developer Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
555 views
in Technique[技术] by (71.8m points)

typescript - Infer object shape from property value

Don't even know if this is possible, so here goes nothing...


type CustomProps = {
    id: string;
    defaultValue: number;
}

function MyComponent(props: CustomProps){
    return <div>my custom fc</div>;
}

type Desired = {
    shape: React.ComponentType<any>
}

const x: Desired = {
    shape: MyComponent,
    // the issue is, to transform x on-the-fly
    // AFTER shape is declared
    // so it can extend the type of props MyComponent can receive
    // basically x becomes `Desired` & `CustomProps`
}

I know I can declare a type param for Desired, infer props, and that would make it work, but... it's not what i'm looking for.

This even possible?


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)
等待大神答复

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to MLink Developer Q&A Community for programmer and developer-Open, Learning and Share
...