0

What would the return type be here?

the paths value is a function that returns an element.

export type iconName = "logo";

type icon = {
    viewBox: string;
    paths: () => ??;
};

const IconSet: Record<iconName, icon> = {
    logo: {
        viewBox: "0 0 20 20",
        paths: () => (<div>...</div>),
    },
};

export { IconSet };

I just want to use it like this in js


export const test = {
  a : () => {return (
    <div>...</div>
    
  )}
}

my package.json file

enter image description here

and tsconfig file here

enter image description here

1 Answer 1

1

You can return JSX.Element from e function that return a JSX element:

type icon = {
    viewBox: string;
    paths: () => JSX.Element;
};

const IconSet: Record<iconName, icon> = {
    logo: {
        viewBox: "0 0 20 20",
        paths: () => (<div>...</div>),
    },
};

Playground Link

Sign up to request clarification or add additional context in comments.

4 Comments

I used that but got an error. errorType 'boolean' is not assignable to type 'Element'.ts(2322) logo.ts(5, 9): The expected type comes from the return type of this signature.
@minami The code in the playground works. Not sure why you are getting an error. If you can post an example with the failure that would help
Yeah... As you said, it works well in Codes and Box. codesandbox.io/s/falling-glitter-ouizt?file=/src/icon.tsx Is it a matter of setting up my project? I uploaded tsconfig.json and package.json. Can you take a look?
I solved this. It was because I used .ts, not .tsx. so foolish.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.