How can I set properties by a given table of properties
This might be a dumb question but Im new on roblox-ts, so, I basically have this function that creates a new roblox object and additionally, you are able to give a table of properties to set with the structure: {propertyName = propertyValue}
example New("Blur", {Size = 10},Camera)
AllProperties type is defined as:
I ran out of ideas, this stills red, if you can also explain me a little I would appreciate it a lot
here's a playground https://roblox-ts.com/playground/#code/C4TwDgpgBAggNnACgJwPaWcAlhAzlAXigGFkIBDYcgIzggEkA7XKxgYzwG0BrCEVAGYkylGnSYty7PAF0AUHIEBXdtlSMoAOQgB3ABRs45XLk3kAthABcUXvyGkKVWg2asOuADRQwaDNjwAfhsAbygePhs7QVgEFHQITBxcGRsVbkZUHUYAX28wcjJGYGCoCXcIAEobcqkOKBC5AEg2dRYoRl1a6UIO3TK3OogDIxMzS08CouBKgG5mpqwhPV8EpLxKhoWmgVRkKAM24HDV-xBx6Hy-RNAANXI4JQgZKBiCrGRcFev13ErNxpNIFNMBsB5wPR6TYEAB8W2BTU6Om6HE4pxu5ws0GMtj4MXgSB+ARSvXRmBA90eEG2OTmCxyzQZTTIwCUyA0SJREHmOSAA

22 Replies
Unknown Userā¢2y ago
Message Not Public
Sign In & Join Server To View
@Noire ā @Dionysusnu so, I somehow managed to set the type for the properties table, it has correct autocompletition, most of that mess for properties type was because I got crazy trying a bunch of things
but still telling me that last error

this works fine

this is the error

also that's how I defied those types
found the solution of adding as never at the end, I guess there's no way to set it with correct types
thanks :DD
yeah I always try to typecheck and workaround with another way, but Im pretty happy with this autocompletition tho, I started liking ts

Playground link
Posted by <@214262712753061889>
Playground link
Posted by <@214262712753061889>
Im not home right now so I can't take a look but I have noticed typescript has a tendency to cut off conditionals where the generic is in a check type
you're iterating over
PairsResult<typeof properties>
not the a
type
you're checking that it's not undefined
casting uses inference, type aliases do not
as for why this is returning [unknown, unknown]
it's because TypeScript is widening to the most possible type
first element gets unknown
from [unknown, defined]
, second element gets it from Exclude<V, undefined>
in ReadonlyArray and ReadonlyMap
V
can be anything, so its constraint is unknown
nothing comes to mind
besides overloading, of course, but I don't think that'd be ideal
Ah!
well, pairs
doesn't quite work either in that case
its fallback still performs a conditional so the code won't know the correct type
the keyof T extends never
special case, not sure its purpose
mmm
I wonder if the switch from defined -> unknown would be particularly breaking
assuming that's the only difference between your implementation and the overloads
it can't, Exclude uses a conditional
NonNullable might, though
it uses an intersection
yeah, NonNullable worksit infers
[unknown, defined]
by doing the keyof T extends never
check on the first overload
so it'll fall over to the second overload for genericsPlayground link
Posted by <@214262712753061889>
mmm
TypeScript isn't able to do assignments like that with plain destructuring, but the generic one probably preserves the relationship
the static one will become
"a" | "b", obj["a" | "b"]
whereas the generic one will become K, V[K]
Playground link
Posted by <@214262712753061889>
mm, unfortunately this seems to deviate from pairs with constrained generics
https://roblox-ts.com/playground/#code/C4TwDgpgBACghgSwE4GcASEA2kkB4AqAfFALxT5QQAewEAdgCYpQBKEcDA9nZiAIJIkcELgR0AZhCRQAaoQBQASAD8UANp0ArgFsARlIA0UAKJUAxpk0MIuGUc2MI4sRAaEAukoBc5SjXpMrOxcPCAAyhDAohJSUADSCirqcUbASJoQnoo+FNS0jMxsHNy8ALJwYNGS0ilQYtWyiapqtaYWVjZ2UA7WznSuHt5QANYQIJzivnkBzP0AblJKzQ7DdJwA7nRGvS4MWT4A3sl1dCNjE+TuPi1GbZbWBC3u9o59A+5QAL5qo+OT+O4ANzyeTWCxwJDQcQOMzABDcKBgRCoABMBD8+UCnF0ACsILDCAAKbF42E+X4XXL+ApQeaxVR06Q5ACUPgAkrQhLpMBAAGIwuHcXAAGU0cHwmjAPNw8GQ6CwOAIhGVwLBmAhUIF8NOSLlaKpmOYJPxwCJxrJ5FZUA5Ujg3L5WqFovFkulahWa022ycu3cKpB0LosO15AgKCiBpmQWKoXKlXDSDEAHMjFo9FJlYS5iyoAclOJONJCWZuOH1HQ4NoIEY5nBLJkoBddais8zmbmQYpPp2C0WS3QyxpK9WoLX6x8m8iUK323mlN3u0A
having conditional pairs as a overload after readonlyset/readonlymap/etc special cases might work?
unions
I think current
pairs
will fail on generics with a union constraint, so it should be fine if the conditional fallback does as well
that leaves known unions which would now iterate correctly using the conditional fallback
sorry, what's this showing?Playground link
Posted by <@214262712753061889>
you're intersecting the tuple
Playground link
Posted by <@259237045593964554>
Playground link
Posted by <@214262712753061889>