kriasoft / universal-router

A simple middleware-style router for isomorphic JavaScript web apps
https://www.kriasoft.com/universal-router/
MIT License
1.7k stars 105 forks source link

Incorrect TypeScript RouteParams Interface #189

Closed plemarquand closed 4 years ago

plemarquand commented 4 years ago

I'm submitting a ...

The interface for RouteParams is:

export interface RouteParams {
  [paramName: string]: string | string[]
}

(https://github.com/kriasoft/universal-router/blob/master/src/UniversalRouter.ts#L39)

However its possible for route parameters to be optional:

const routes = [
  { name: 'user', path: '/user/:username?' }
]

Leading me to think the RouteParams interface should look like:

export interface RouteParams {
  [paramName: string]: string | string[] | undefined;
}
frenzzy commented 4 years ago

If parameter is present its value always a string and can't be undefined (CodePen):

import UniversalRouter from 'universal-router';

const router = new UniversalRouter({
  path: '/user/:username?',
  action: ({ params }) => JSON.stringify(params)
})

router.resolve('/user').then(console.log)      // => {}
router.resolve('/user/').then(console.log)     // => {}
router.resolve('/user/demo').then(console.log) // => {"username":"demo"}

i.e. { "username": undefined } is not possible.

plemarquand commented 4 years ago

You're right, I should be guarding property existence on maps with hasOwnProperty instead. Closing.