#How to base the type of these properties based on the specific class's properties?

18 messages · Page 1 of 1 (latest)

tranquil igloo
#

I've got a class with a static defaultData that returns some default data for constructing this class. Is there any way for me to type the response of this as "Partial from Foo with these explicit properties?"

Something like Foo["_id", "default", "modifiers", "name"] where it gets the types of those values from the Foo class?

buoyant spokeBOT
#
claudekennilol#0

Preview:```ts
declare class Foo {
_id?: string
default: boolean
modifiers: number[]
name: string

plus: any
other: any
props: any

static get defaultData(): {
_id: string
default: false
modifiers: []
name:
...```

shadow wyvern
#

Required<Pick<Foo, "_id" | "default" | "modifiers" | "name">>

tranquil igloo
shadow wyvern
#

just replace it with the current type...

tranquil igloo
#

That's the part of the problem - it doesn't have a current type and I don't know how to add it

#

Or rather, I want it to be typed with the info of the specific defaults

#

So I'm asking how to both type it and give those properties specific values

shadow wyvern
#

you've typed it as below in the playground, are you saying you want to return this typed with these values?

{
    _id: string;
    default: false;
    modifiers: [];
    name: '';
};
tranquil igloo
#

If I were writing the real function (instead of just the type definition), I'd write it like this

get defaultData(): Required<Pick<Foo, "_id" | "default" | "modifiers" | "name">> {
  return {
    _id: "random id";
    default: false;
    modifiers: [];
    name: '';
  };
}

So I'm trying to do something similar with the type definition

shadow wyvern
#

and you want the actual values in the type?

tranquil igloo
#

Since the defaultData getter is returning specific data, but I also want to type it based on Foo (e.g. otherwise modifiers won't imply it's an empty array of numbers, but just an empty array)

tranquil igloo
shadow wyvern
#

is this not what you want?

tranquil igloo
#

Yup, that appears to be what I was missing, thanks 👍

#

resolved!