ziggy icon indicating copy to clipboard operation
ziggy copied to clipboard

TypeError when optional params omitted from array

Open bakerkretzmar opened this issue 1 year ago • 1 comments

Given this route:

const routes = {
    'posts.comments.show': {
        uri: 'posts/{post}/comments/{comment?}',
        methods: ['GET', 'HEAD'],
        parameters: ['post', 'comment'],
    },
};

Which generates these types:

interface RouteList {
    'posts.comments.show': [
        { name: 'post'; required: true },
        { name: 'comment'; required: false },
    ];
}

These valid route() calls, which should not cause any type errors, do:

route('posts.comments.show', [2]);
route('posts.comments.show', ['foo']);
route('posts.comments.show', [{ id: 2 }]);

The issue is that our type for an array of parameters doesn't specify any of them as optional (except additional unknown params after the ones we're expecting), so e.g. passing an array with less than 2 items will error if the route has 2 parameters, even if some of them are optional.

After #697 I'm hoping this will be possible to fix.

bakerkretzmar avatar Mar 25 '24 19:03 bakerkretzmar

This seems like it's still an issue. For the time being I just switched to using an object instead of an array.

UltraWelfare avatar Mar 19 '25 15:03 UltraWelfare