Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Angular 14 pathMatch Type 'string' is not assignable to type '"full" | "prefix"' Error

Tags:

angular14

Just upgraded to Angular 14, getting error:

Types of property 'pathMatch' are incompatible.
        Type 'string' is not assignable to type '"full" | "prefix"'.

Calling from:

import { Routes as routes } from './routes';
@NgModule({
  imports: [
    RouterModule.forChild(routes)
    ]...})

with routes being

export const Routes = [
  {
    path: '',
    redirectTo: '/signup',
    pathMatch: 'full'
  }]

The whole thing worked perfectly on Angular 10. Any ideas ?

like image 938
paulrusu Avatar asked Aug 23 '26 22:08

paulrusu


2 Answers

You can create a type and cast to it:

type PathMatch = "full" | "prefix" | undefined;

const routes = [
  {
    path: 'achievements',
    component: AchievementOverviewComponent,
    pathMatch: 'full' as PathMatch
  }
]

More info in a related answer: Typescript Type 'string' is not assignable to type.

like image 111
Wim Ombelets Avatar answered Aug 27 '26 00:08

Wim Ombelets


Just define routes as Routes, so TypeScript will know that pathMatch is 'full', 'prefix' or undefined.

This is better than the currently accepted (and working) answer, because it also type checks the other properties.

import { Routes } from '@angular/router';

export const routes: Routes = [
  {
    path: '',
    redirectTo: '/signup',
    pathMatch: 'full'
  }]
like image 32
bnGG Avatar answered Aug 27 '26 00:08

bnGG



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!