#How to preserve types?

10 messages · Page 1 of 1 (latest)

pastel pawn
#

I need to preserve column types and column id has to be either string or keyof data. Spent few hours, cant make it work. I would be glad for any help.

    date: string;
}

type ColumnId<T> = keyof T | (string & {});
type BaseColumn<T> = {
    id: ColumnId<T>;
    title: string;
}

const columns2: BaseColumn<DataRow>[] = [
    {
        id: 'checkbox',
        title: 'Id',
    },
    {
        id: 'date',
        title: 'ID',
    },
]

class TestClass<T, C extends BaseColumn<T> = BaseColumn<T>> {
    data: T[] = []
    columns: C[] = []

    constructor(data: T[], columns: C[]) {
        this.data = data;
        this.columns = columns;
    }
}

const test = new TestClass<DataRow>([], columns2);

for (const column of test.columns) {
    // just data types showing, no custom checkbox type checking
    if (column.id === '') {
        console.log('This is a checkbox column');
    } else {
        console.log(`This is a ${column.id} column`);
    }
}```
prisma nova
#

what exactly isn't working here?

#

do you want columns2 to have more specific types?

#

the type annotation overrides any more specific type info, use satisfies

fossil spadeBOT
#
that_guy977#0

Preview:```ts
export type DataRow = {
date: string;
}

type ColumnId<T> = keyof T | (string & {});
type BaseColumn<T> = {
id: ColumnId<T>;
title: string;
}

const columns2 = [
{
id: 'checkbox',
title: 'Id',
},
{
id: 'date',
...```

pastel pawn
#

typechecking on columns accessed from instance of a class

#

it should return checkbox type too

prisma nova
#

checkbox is lost because of the type annotations, try checking the playground

pastel pawn
#

oh