Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Typescript: cannot import Item component - type is imported instead #21

Open
goliney opened this issue Jul 23, 2019 · 8 comments
Open

Comments

@goliney
Copy link

goliney commented Jul 23, 2019

index.test-d.tsx exports types and default value (InkSelectInput):

/**
* Select input component for Ink
*/
export default class InkSelectInput extends React.Component<InkSelectInputProps> {}

However, it doesn't export Item and Indicator components. Because of that, an attempt to import Item from ink-select-input ends up with:

image

Error:

error TS2693: 'Item' only refers to a type, but is being used as a value here.

Code snippet used:

import React from 'react';
import SelectInput, { Item, InkSelectInputProps } from 'ink-select-input';

export function ChoicesComponent({ itemComponent = Item, items, onSubmit }: Props) {
  return <SelectInput 
    itemComponent={itemComponent} 
    items={items} 
    onSelect={onSubmit} 
  />;
}

type ChoiceValue = any;

interface ChoiceOption {
  key?: string;
  label: string;
  value: ChoiceValue;
}

interface Choices {
  message?: string;
  items: ChoiceOption[];
}

interface Props extends Choices {
  onSubmit: () => void;
  itemComponent?: InkSelectInputProps['itemComponent'];
}
@vadimdemedes
Copy link
Owner

This component does export Item type, there's a mistake in your example:

export function ChoicesComponent({ itemComponent = Item, items, onSubmit }: Props) {

itemComponent = Item means Item is a default value for itemComponent, but it's only a TS type, it doesn't exist in runtime. Instead, you should declare itemComponent: Item in Props type.

@goliney
Copy link
Author

goliney commented Jul 29, 2019

@vadimdemedes you got my intentions wrong. I want to import a component, not a type, and use it as a default parameter. But I can’t, because a type is imported instead.

@vadimdemedes
Copy link
Owner

Hm, what would be a solution for this?

@vadimdemedes vadimdemedes reopened this Sep 22, 2019
@guyellis
Copy link

To get Item as a type I had to setup my imports as such:

import SelectInput from 'ink-select-input';
import { Item } from 'ink-select-input/build/SelectInput';

@eugirdor
Copy link

The issue seems to be that the export in index.ts is exporting the Item component form Item.ts instead of the Item interface from SelectInput.ts

export {default as Item, Props as ItemProps} from './Item';

I think this should actually be:

export { Item } from './SelectInput';
export { Props as ItemProps } from './Item';

@vadimdemedes
Copy link
Owner

I think you can solve this via:

import {FC, ComponentType} from 'react';
import {ItemProps} from 'ink-select-input';

interface Props {
  itemComponent: ComponentType<ItemProps>
}

const MyCustomComponent: FC<Props> = ({itemComponent}) => ...;

@gustawdaniel
Copy link

@guyellis I tried

import SelectInput from 'ink-select-input';
import { Item } from 'ink-select-input/build/SelectInput';

and have error

TS2307: Cannot find module 'ink-select-input/build/SelectInput' or its corresponding type declarations.

@gustawdaniel
Copy link

Ok. I had

type Environment = 'beta' | 'production';

  const handleSelect = (item: { label: Environment, value: Environment}) => {
    props.setEnv(item.value)
  };

  const items:Array<{ label: Environment, value: Environment}> = [
    {
      label: 'beta',
      value: 'beta'
    },
    {
      label: 'production',
      value: 'production'
    },
  ];

  return <SelectInput items={items} onSelect={handleSelect} />;

and fixed by setting the label as a string

  const handleSelect = (item: { label: string, value: Environment}) => {
    props.setEnv(item.value)
  };

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

5 participants