How do i specify keyof

How do i specify Name type so it contains only properties of type number?
No description
86 Replies
Tverksaac 2.0
Tverksaac 2.0OP4mo ago
i tried to extract but it didnt work Name extends Extract<keyof WritableInstanceProperties<ConnectedInstance>, number>, also tried ExtractKeys
alihsaas
alihsaas4mo ago
type IncludeOfType<Object, Type> = {
[K in keyof Object as Object[K] extends Type ? K : never]: Object[K];
};

type T1 = IncludeOfType<{
Name: string,
Width: number,
}, number>
type IncludeOfType<Object, Type> = {
[K in keyof Object as Object[K] extends Type ? K : never]: Object[K];
};

type T1 = IncludeOfType<{
Name: string,
Width: number,
}, number>
can also be done using Pick<>
Tverksaac 2.0
Tverksaac 2.0OP4mo ago
thanks you but i realised that keyof WritableInstanceProperties<ConnectedInstance> contains only strings how can i determine if property have number value or other
alihsaas
alihsaas4mo ago
T extends number ? "T is a number" : "T is not a number";
T extends number ? "T is a number" : "T is not a number";
Tverksaac 2.0
Tverksaac 2.0OP4mo ago
where should i put this? what is this
alihsaas
alihsaas4mo ago
wherever you want T can be whatever
Tverksaac 2.0
Tverksaac 2.0OP4mo ago
but what does it mean? why it contains strings "T is a number"
alihsaas
alihsaas4mo ago
it's a conditional type, it checks if T can be assigned to number, if yes it reuturns the first type, otherwise the second here the returned types are literal strings, but they can be whatever to fit what you want
Tverksaac 2.0
Tverksaac 2.0OP4mo ago
im not sure how it would help me making the type of number properties
alihsaas
alihsaas4mo ago
you asked how can i determine if property have number value or other
Tverksaac 2.0
Tverksaac 2.0OP4mo ago
ok so when i do Name extends WritableInstanceProperties<ConnectedInstance> how Name type would look? Cuz know i dont really understand it should be like ["PropName1", "PropName2"...]
alihsaas
alihsaas4mo ago
huh
Tverksaac 2.0
Tverksaac 2.0OP4mo ago
aa like WritableInstanceProperties<ConnectedInstance> returns a type right?
alihsaas
alihsaas4mo ago
yah..
Tverksaac 2.0
Tverksaac 2.0OP4mo ago
so how it look like
Name extends WritableInstanceProperties<ConnectedInstance>
typeof Name = ["DisplayName", "ClassName"...etc]
Name extends WritableInstanceProperties<ConnectedInstance>
typeof Name = ["DisplayName", "ClassName"...etc]
alihsaas
alihsaas4mo ago
you want an array of them?
Tverksaac 2.0
Tverksaac 2.0OP4mo ago
or maybe
typeof Name = ["DisplayName": string..etc]
typeof Name = ["DisplayName": string..etc]
i dont want an array i just want to know what exactly returns this
alihsaas
alihsaas4mo ago
No description
Tverksaac 2.0
Tverksaac 2.0OP4mo ago
oh tysm now i understand
Tverksaac 2.0
Tverksaac 2.0OP4mo ago
..why it cant index it?
No description
alihsaas
alihsaas4mo ago
you forgot keyof before InstanceOfType InstanceOfType<...> is an Object type
Tverksaac 2.0
Tverksaac 2.0OP4mo ago
oh yes but now theres another error
Tverksaac 2.0
Tverksaac 2.0OP4mo ago
this is error i was running for using include
No description
Tverksaac 2.0
Tverksaac 2.0OP4mo ago
same problem now yo is there any way to print type
alihsaas
alihsaas4mo ago
not getting that error on my end
Tverksaac 2.0
Tverksaac 2.0OP4mo ago
print(typeof Name) would work?
alihsaas
alihsaas4mo ago
no ... 😅
Tverksaac 2.0
Tverksaac 2.0OP4mo ago
right its in roblox how can i see final type then?
alihsaas
alihsaas4mo ago
types dont exit during run time
Jolly Pizza
Jolly Pizza4mo ago
hover oh wait at runtime idk
Tverksaac 2.0
Tverksaac 2.0OP4mo ago
it return very bad type
Tverksaac 2.0
Tverksaac 2.0OP4mo ago
No description
alihsaas
alihsaas4mo ago
can you show where the error is
Tverksaac 2.0
Tverksaac 2.0OP4mo ago
i want like exact values see in playground
alihsaas
alihsaas4mo ago
yah you wont get any exact values cause it's a generic
Tverksaac 2.0
Tverksaac 2.0OP4mo ago
add keyof keyword before
alihsaas
alihsaas4mo ago
i did and i dont get any errors (other than that related to SeperatedProperty)
Tverksaac 2.0
Tverksaac 2.0OP4mo ago
lemme see
alihsaas
alihsaas4mo ago
No description
Tverksaac 2.0
Tverksaac 2.0OP4mo ago
oh thats because you need an property replace total.Get() with any number same error appears i think that ts dont understand wat IncludeOfType returns
alihsaas
alihsaas4mo ago
exactly what WritableInstanceProperty returns, but just property with number values
alihsaas
alihsaas4mo ago
No description
Tverksaac 2.0
Tverksaac 2.0OP4mo ago
oh what arbitrary type means?
alihsaas
alihsaas4mo ago
youll have to do
No description
Tverksaac 2.0
Tverksaac 2.0OP4mo ago
uhhh but this is type casting i can do as never with the same result
alihsaas
alihsaas4mo ago
hmm actually lemme try something
Tverksaac 2.0
Tverksaac 2.0OP4mo ago
k maybe typescript cant realize that ConnectedInstance[Name] is 100% number because of generics
alihsaas
alihsaas4mo ago
yah that's exactly the issu e yaaah you have to type cast it idk how else you can fix this
Tverksaac 2.0
Tverksaac 2.0OP4mo ago
uhhh idk i dont believe it cant be typed
alihsaas
alihsaas4mo ago
and dont ever use as never in scenarios like this, i only use it in places where things should be impossible or there should be an error
Tverksaac 2.0
Tverksaac 2.0OP4mo ago
ikik thanks you
Tverksaac 2.0
Tverksaac 2.0OP4mo ago
idk why but when i try to save in terminal it outputs error but vs code dont see an errors
No description
Tverksaac 2.0
Tverksaac 2.0OP4mo ago
No description
Tverksaac 2.0
Tverksaac 2.0OP4mo ago
oh i fixed it
wAD
wAD4mo ago
so unnecessarily complicated ExtractKeys<WritableInstanceProperties<T>, number>
alihsaas
alihsaas4mo ago
Missed it in the utility types mb But you also don't need the keyof
Tverksaac 2.0
Tverksaac 2.0OP4mo ago
Ohhh i tried to do it first but added keyof Thanks you
Tverksaac 2.0
Tverksaac 2.0OP4mo ago
ohh it still shows an error
No description
Tverksaac 2.0
Tverksaac 2.0OP4mo ago
export class ConnectedStat<
ConnectedInstance extends Instance,
Name extends ExtractKeys<WritableInstanceProperties<ConnectedInstance>, number>,
> extends SeparatedStat {
instance: ConnectedInstance;
conected_to: Name;

constructor(Name: string, Value: number, ConnectToInstance: ConnectedInstance, InstancePropertyName: Name) {
super(Name, Value);

this.instance = ConnectToInstance;
this.instance[InstancePropertyName] = this.Total.Get();
this.conected_to = InstancePropertyName;

this.Total.changed.Connect(() => {
this.instance[InstancePropertyName] = this.Total.Get();
});
this.instance.GetPropertyChangedSignal(InstancePropertyName).Connect(() => {
this.instance[InstancePropertyName] = this.Total.Get();
});
}
}
export class ConnectedStat<
ConnectedInstance extends Instance,
Name extends ExtractKeys<WritableInstanceProperties<ConnectedInstance>, number>,
> extends SeparatedStat {
instance: ConnectedInstance;
conected_to: Name;

constructor(Name: string, Value: number, ConnectToInstance: ConnectedInstance, InstancePropertyName: Name) {
super(Name, Value);

this.instance = ConnectToInstance;
this.instance[InstancePropertyName] = this.Total.Get();
this.conected_to = InstancePropertyName;

this.Total.changed.Connect(() => {
this.instance[InstancePropertyName] = this.Total.Get();
});
this.instance.GetPropertyChangedSignal(InstancePropertyName).Connect(() => {
this.instance[InstancePropertyName] = this.Total.Get();
});
}
}
i cant really give playground cuz it need a property class
alihsaas
alihsaas4mo ago
cause it's a type that does the same thing as the one i gave you, but it is built in instead of what i gave you
Tverksaac 2.0
Tverksaac 2.0OP4mo ago
if i add keyof before extract keys it wont work too right
wAD
wAD4mo ago
because Name: string switch to Name: Name
Tverksaac 2.0
Tverksaac 2.0OP4mo ago
nooo Name is is just a string you passing to constructor
wAD
wAD4mo ago
oh
Tverksaac 2.0
Tverksaac 2.0OP4mo ago
it just name of stat ye
wAD
wAD4mo ago
can u give a playground repro
Tverksaac 2.0
Tverksaac 2.0OP4mo ago
how can i make link shorter its too long😃
wAD
wAD4mo ago
send as file @Tverksaac 2.0 faster
wAD
wAD4mo ago
i meant send link as file
Tverksaac 2.0
Tverksaac 2.0OP4mo ago
heree
wAD
wAD4mo ago
@Tverksaac 2.0 yeah i think typescript can't ensure this index in a generic context never cast is the only way to go i tried or actualy
Tverksaac 2.0
Tverksaac 2.0OP4mo ago
ConnectedInstance[Name] can be casted but nothing really changes ye
wAD
wAD4mo ago
hold on link roblox-ts bot in question @Tverksaac 2.0 cast like this way better
Tverksaac 2.0
Tverksaac 2.0OP4mo ago
oh ok thanks you why tho? oh nvm i understood
alihsaas
alihsaas4mo ago
its same as ConnectedInstance[Name] but created a type alias so not repeated
Tverksaac 2.0
Tverksaac 2.0OP4mo ago
yeye thanks you guys so much❤️
wAD
wAD4mo ago
need to pr index type assertion to typescript so asserts like this: assert(typeIs(a[b]), "number") affect the whole scope not just this one value
Tverksaac 2.0
Tverksaac 2.0OP4mo ago
wait what oh your talking about what need to be added?
wAD
wAD4mo ago
yeah
Tverksaac 2.0
Tverksaac 2.0OP4mo ago
oh i see then

Did you find this page helpful?