#How to base the type of these properties based on the specific class's properties?
18 messages · Page 1 of 1 (latest)
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:
...```
Required<Pick<Foo, "_id" | "default" | "modifiers" | "name">>
I don't know what to do with that info
just replace it with the current type...
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
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: '';
};
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
and you want the actual values in the type?
Correct
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)
use as const https://www.typescriptlang.org/docs/handbook/release-notes/typescript-3-4.html#const-assertions
TypeScript 3.4 Release Notes
I don't see how that allows me to type it within the type description
is this not what you want?