Because the template checker gives an error/warning when you are using custom colors, there is a hacky way around it. This is only for the real desperate people who like things to be strict strict strict and have proper hinting from the IDE :)
First step is to create your own ambient declaration. I've added mine to the polyfills.ts
, because I'm a bit lazy:
declare module '@angular/material/core/common-behaviors/color' {
interface CanColor {
// @ts-ignore
color: CustomThemePalette;
}
}
The @ts-ignore
is necessary, otherwise you'll get the following error:
TS2717: Subsequent property declarations must have the same type. Property 'color' must be of type 'ThemePalette', but here has type 'CustomThemePalette'.
Then you can define your custom theme palette:
import type { ThemePalette } from '@angular/material/core/common-behaviors/color';
export type CustomThemePalette = ThemePalette | 'secondary' | 'success' | 'alert';
Now finally you can use those custom colors in your template, and your IDE will hint them and notify you when you've mistyped them. Victory!

note: obviously things will go south this way, when material decides to change their CanColor
interface or move it around, but I suppose it would be relatively easy to adapt to such changes