#"complex types" working as expected but type result not shown properly

23 messages · Page 1 of 1 (latest)

tacit oasis
#

everything is working correctly except that
the output function returns the StructToObject but the result is still "wrapped". I want it to show the type that you would get when you use typeof on the result

interface StructuredType<T> {}

const bool: StructuredType<boolean> = {};
const uint8: StructuredType<number> = {};

interface Property { [key: string]: StructuredType<any>; };

type InferStructuredType<T> = T extends StructuredType<infer U> ? U : never;

type StructToObject<T extends readonly Property[]> = {
    [K in T[number] as keyof K]: InferStructuredType<K[keyof K]>;
  }

class Structured<T extends readonly Property[]> {
    constructor(public struct: T) {}

    output(): StructToObject<T> {
       
        // @ts-ignore
        return {};
    }
}

const b = new Structured([{x: bool}, {y: uint8}] as const);
// shows incorrectly
const result = b.output()

// shows correctly
type bruh = typeof result
supple raftBOT
#
mqix#0

Preview:```ts
interface StructuredType<T> {}

const bool: StructuredType<boolean> = {}
const uint8: StructuredType<number> = {}

interface Property {
[key: string]: StructuredType<any>
}

type InferStructuredType<T> = T extends StructuredType<
infer U

? U
: never

type StructToObject<T extends readonly Property[]> = {
...```

tacit oasis
#
type Prettify<T> = {
    [K in keyof T]: T[K]
} & {}

class Structured<T extends readonly Property[]> {
    constructor(public struct: T) {}

    output(): Prettify<StructToObject<T>> {
        // @ts-ignore
        return {}
    }

#

found it :)

#

!solved

subtle lintel
#

@tacit oasis If you have a mapped type like StructToObject you can juse add & {} at the end if you prefer

type StructToObject<T extends readonly Property[]> = {
    [K in T[number] as keyof K]: InferStructuredType<K[keyof K]>;
} & {}
supple raftBOT
#
interface StructuredType<T> {}

const bool: StructuredType<boolean> = {};
const uint8: StructuredType<number> = {};

interface Property { [key: string]: StructuredType<any>; };

type InferStructuredType<T> = T extends StructuredType<infer U> ? U : never;

type StructToObject<T extends readonly Property[]> = {
    [K in T[number] as keyof K]: InferStructuredType<K[keyof K]>;
} & {}


class Structured<T extends readonly Property[]> {
    constructor(public struct: T) {}

    output(): StructToObject<T> {
       
        // @ts-ignore
        return {};
    }
}

const b = new Structured([{x: bool}, {y: uint8}] as const);

const result = b.output()
//    ^? - const result: {
//        x: boolean;
//        y: number;
//    }
tacit oasis
#

do you know by any change how i can make that property only can have 1 key?

subtle lintel
#

where?

#
const b = new Structured([{x: bool}, {y: uint8}]);

Only 1 entry here?

tacit oasis
#

ye so that

const b = new Structured([{x: bool, b: bool}, {y: uint8}]);

is invalid

subtle lintel
#
class Structured<T extends [Property]> {
#

or

class Structured<T extends readonly [Property]> {
#

This will also prevent passing an array of 0 items

tacit oasis
#

now i can only pass in 1 item in the array

i mean that i can have property in the property type

subtle lintel
#

You mean they all have to be the same type?

#

oh I see

#

you want to stop b: bool

tacit oasis
#

yes

subtle lintel
#

You can't really, because TS is structually typed

#

types say what the thing has, not what it does not have