将可区分的联合划分为有和没有 属性 类型的方法
Way to segment discriminated union into types with and without property
假设我有这样一个受歧视的工会:
type Route = HomeRoute | ProfileRoute | BlogRoute;
type HomeRoute = {
route: '/home'
}
type ProfileRoute = {
route: '/profile/:userId',
params: {
userId: string;
}
}
type BlogRoute = {
route: '/blog/:teamId',
params: {
teamId: string;
}
}
我有一个对 Route
对象进行操作的函数,如果它们有参数,则有一些可选逻辑:
function processRoute(route: Route) {
if ('params' in route) {
const { params } = route; // <-- this errors
}
}
似乎没有办法(我可以看到)在不添加 any
注释的情况下检查 params
...
function paramsInRoute(route: any): route is { params: {[key: string]: string} } {
return ('params' in route);
}
function processRoute(route: Route) {
if ('params' in route) {
const { params } = route; // <-- this errors
}
if (paramsInRoute(route)) {
const { params } = route; // <-- this typechecks
}
}
有没有办法在不转换为任何(在 paramsInRoute
的参数中)的情况下执行上述操作?
就我个人而言,我很乐意使用你的类型保护:
function paramsInRoute(route: any): route is { params: {[key: string]: string} } {
return ('params' in route);
}
因为 TypeScript 肯定会将传入的 Route
对象缩小为 ProfileRoute
| BlogRoute
if (paramsInRoute(route)) {
const notHomeRouteAnymore: ProfileRoute | BlogRoute = route; // no error
}
但是如果您担心有人这样做:
const notRoute = "This is not a route";
if (paramsInRoute(notRoute)) { notRoute.params.oops = 'what' };
那么你可以这样做:
function paramsInRoute(route: Route): route is Route & { params: {[key: string]: string} } {
return ('params' in route);
}
它进行相同的缩小但防止错误调用:
paramsInRoute(notRoute); // error
希望对您有所帮助;祝你好运!
假设我有这样一个受歧视的工会:
type Route = HomeRoute | ProfileRoute | BlogRoute;
type HomeRoute = {
route: '/home'
}
type ProfileRoute = {
route: '/profile/:userId',
params: {
userId: string;
}
}
type BlogRoute = {
route: '/blog/:teamId',
params: {
teamId: string;
}
}
我有一个对 Route
对象进行操作的函数,如果它们有参数,则有一些可选逻辑:
function processRoute(route: Route) {
if ('params' in route) {
const { params } = route; // <-- this errors
}
}
似乎没有办法(我可以看到)在不添加 any
注释的情况下检查 params
...
function paramsInRoute(route: any): route is { params: {[key: string]: string} } {
return ('params' in route);
}
function processRoute(route: Route) {
if ('params' in route) {
const { params } = route; // <-- this errors
}
if (paramsInRoute(route)) {
const { params } = route; // <-- this typechecks
}
}
有没有办法在不转换为任何(在 paramsInRoute
的参数中)的情况下执行上述操作?
就我个人而言,我很乐意使用你的类型保护:
function paramsInRoute(route: any): route is { params: {[key: string]: string} } {
return ('params' in route);
}
因为 TypeScript 肯定会将传入的 Route
对象缩小为 ProfileRoute
| BlogRoute
if (paramsInRoute(route)) {
const notHomeRouteAnymore: ProfileRoute | BlogRoute = route; // no error
}
但是如果您担心有人这样做:
const notRoute = "This is not a route";
if (paramsInRoute(notRoute)) { notRoute.params.oops = 'what' };
那么你可以这样做:
function paramsInRoute(route: Route): route is Route & { params: {[key: string]: string} } {
return ('params' in route);
}
它进行相同的缩小但防止错误调用:
paramsInRoute(notRoute); // error
希望对您有所帮助;祝你好运!