Hookyns / tst-reflect

Advanced TypeScript runtime reflection system
MIT License
328 stars 11 forks source link

There is no way to get Record type arguments #46

Closed ciricc closed 1 year ago

ciricc commented 2 years ago

Hi. I've tried to get Record generic types, but nothing returned.

const t = getType<Record<string, boolean>>()
console.log(t.getTypeParameters(), t.getTypeArguments()) // [], []
Hookyns commented 1 year ago

I'm sorry @ciricc, idk how but I missed this issue.


This is a little odd case. type is always resolved to its definition but in case of Record, its definition is an empty object allowing any property of given type (more specific any). I don't know how to handle it. I am able to handle it and force it to be type Record with given generic arguments (like Array) but there is a huge amount of similar types...

There is a workaround:

type Schema2 = {
    fields: {
        [prop: string]: SchemaFields
    }
}

const indexItemType = getType<Schema2>()
    .getProperties()
    .find(prop => prop.name == "fields").type
    .getProperties()
    .find(prop => prop.index).type;
console.log(indexItemType.name);

It's a special "__index" property.

ciricc commented 1 year ago

@Hookyns , ok, thank you for reply.

Hookyns commented 1 year ago

@ciricc Test tst-reflect-transformer@0.11.1 and tst-reflect@0.11.0.

Records, Omit and other features creating type aliases should be supported. Combination of properties and indexes is generated. Indexes are no more part of properties and property.index was removed.

See type.getProperties(), type.getIndexes().

ciricc commented 1 year ago

@Hookyns Thank you. I will check it out

Hookyns commented 1 year ago

Record Record<string, SchemaFields> will be generated as index [key: string]: SchemaFields, so:

type T = Record<string, SchemaFields>;
const t = getType<T>();
const index = t.getIndexes()[0];

console.log(index.keyType.name); // string
console.log(index.type.name); // SchemaFields