#Best way to enforce type safety with complex types when using generics

1 messages · Page 1 of 1 (latest)

charred shale
#

I have a bigger project where routing configs have to be defined in multiple places, e.g. :

const routes = [
    {
        path: "/",
        name: "home",
        titleMsg: "Home",
        categoryId: "home",
    },
    {
        path: "/:id",
        name: "path-route",
        titleMsg: "title", // should error
        categoryId: "about", // should error
    }
];

It should be to enforce that if the string path includes a colon some other properties (titleMsg and categoryId) should be disallowed.
So far I have some generic types that can handle this, but I am unsure how we can use these types because they require a generic type to be passed to work.

Is it possible to have simple type that can handle mapped tuples in a child property?
If not what would be the best way to enforce this? Having a noop function to have type inference?

https://www.typescriptlang.org/play?#code/FAFwngDgpgBASgewK4igCQIYGcAKGBOGAtlgDwBqGANklADQwAq+tDAYtVlAHwwC8MSjVhQAHqgB2AEywwABgBIA3lhD4AlhIDmAXwBcy1Ru065MAPxMWsPTA5UuAbmChIsPFqiIUUUnhAAFjBikjIwRppaMAA+MEjSUABmmlBS-OFqkbwC3qiYuATEZMAwpTD+AXQlZR5eyKgA6uqBeIQkfhiB3FVlMABC2FC1ub4V3MDczq7Q-YPD9aOdQSFQ0rIR2jFxCckSqdkwStWlAMadUFoI+GAAklLmthtazr0gzVRQALJYWg8Zxs9jjAJMQoI9MtoXmUIEs-hVnDopuAZvMfE0WoV2jhguJVmEnlt4lIkikpAcAPJEZqkAZcVGoPzdGAAIjeIA+3y0zK2zLOqEu1zuzMmLgA9KKYEQMGAAEawQLqWRnCQwADuVwA1uEEEQoAEEKrzMAxBAriAYMjYIwoKoAKpYSKfDAQaBSRhICAfUiMHGhWS0oYYTwjUgYCRgbgAbQAugdI0CAHRJo69XqRm4wTQwDVQMAIRJMaO2em+Rjp6OR5kwwLM2NQ0o6YDRqYms0WtzweqRADCCAkySiAhTZT5Fyu6httiUmak4IBjhgVAwcqonLnkQXOhj9ZgMM8WGLQbqPm3wERLmAooAVFeYCVbzdZM1dwgsA6ZR8LQgYAEMAA3WAHSIT15Q7AIoHweVf3NZUfzDKRPywfVVWCfB8CuWRwMgo0YAfAsJAQc1VWgtVkCoNI5QtcCYDlVQ1WlL9gn7K4TigxUcKvUVgBOPs6PQnxe37dQtFsXIez7Ad0mHU5zgFCcDxgGMemhI8FPjVMymkjSVMCWxmVFZllO0soQV1PT9V1QygWMtkOR+PS0B1KArOM3pRwFW5ZxZCznKM1MdD83otOM6sAj00U9HUKQXNc0pTLBFlQoAWn41AYti2yvnsllMsMmBxRgUg+AEJCyLSABROA4HJOBrO09yrk8vTl3qPKCqKkr9SQciYEq6rauMxtemjM9HCAA

mossy snowBOT
#

@charred shale Here's a shortened URL of your playground link! You can remove the full link from your message.

john_thunder#0

Preview:```ts
type RouteHasParams<Value, True, False> =
Value extends ${string}:${string} ? True : False

type PageRoute<
Path extends string | undefined = string

= RouteHasParams<
Path,
PageRouteWithParams<Path>,
BasePageRoute<Path>

type BasePageRoute<Path extends string | u
...```