gajus / table

Formats data into a string table.
Other
894 stars 77 forks source link

Alignment is not working #211

Closed luzianscherrer closed 1 year ago

luzianscherrer commented 1 year ago

I'm getting the following error when running the alignment sample from the README:

error TS2345: Argument of type '{ columnDefault: { width: number; }; columns: { alignment: string; }[]; }' is not assignable to parameter of type 'TableUserConfig'.
  Type '{ columnDefault: { width: number; }; columns: { alignment: string; }[]; }' is not assignable to type 'BaseUserConfig'.
    Types of property 'columns' are incompatible.
      Type '{ alignment: string; }[]' is not assignable to type 'Indexable<ColumnUserConfig>'.
        'number' index signatures are incompatible.
          Type '{ alignment: string; }' is not assignable to type 'ColumnUserConfig'.
            Type '{ alignment: string; }' is not assignable to type 'CellUserConfig'.
              Types of property 'alignment' are incompatible.
                Type 'string' is not assignable to type 'Alignment | undefined'.

                 console.log(table(data, config));

This is the code that I'm using:

const data = [
  ['0A', '0B', '0C', '0D 0E 0F'],
  ['1A', '1B', '1C', '1D 1E 1F'],
  ['2A', '2B', '2C', '2D 2E 2F'],
];

const config = {
  columnDefault: {
    width: 10,
  },
  columns: [
    { alignment: 'left' },
    { alignment: 'center' },
    { alignment: 'right' },
    { alignment: 'justify' }
  ],
};

console.log(table(data, config));

Versions used:

nam-hle commented 1 year ago

The error is gone if you add TableUserConfig type for the config variable. I'm not sure if this is what we can fix. Do you have any suggestions?

luzianscherrer commented 1 year ago

Yep, I can confirm that. For me it's fine, but I think the examples should be updated accordingly.

That would be:

import { table, TableUserConfig } from 'table';

const data = [
  ['0A', '0B', '0C', '0D 0E 0F'],
  ['1A', '1B', '1C', '1D 1E 1F'],
  ['2A', '2B', '2C', '2D 2E 2F'],
];

const config: TableUserConfig = {
  columnDefault: {
    width: 10,
  },
  columns: [
    { alignment: 'left' },
    { alignment: 'center' },
    { alignment: 'right' },
    { alignment: 'justify' }
  ],
};

console.log(table(data, config));
nam-hle commented 1 year ago

Thanks. I will update it soon.