#Functional Angular and the satisfies keyword

11 messages · Page 1 of 1 (latest)

low sluice
#

With the more recent versions of Angular, a number of aspects can have functions instead of classes, with inject() as a replacement for constructor injection.
But so far (if that ever changes (probably not)) TS does not support the satisfies keyword with function, so I'm curious how people achieve something along the lines of:

const myRouteResolveThing = ({ queryParamMap }, _, someService = inject(SomeService)) => {
  // ...
} satisfies ResolveFn<T>;

Of course you could do

const myRouteResolveThing: ResolveFn<T> = ({ queryParamMap }, _, someService = inject(SomeService)) => {
  // ...
};

But then you "lose" the 3rd param when you want to unit test the function 🤔

chrome umbra
#

I've been doing the latter, but I do extra injections at the top inside the body of the function, not passed in as parameters, so I don't lose any information by doing the latter.
Passing services in as parameters is really neat, but I don't know how to address your issue

low sluice
#

Well if you do the inject in the body itself, you can no longer pass a mock

#
  • side effects are sad
topaz lava
#

But so far (if that ever changes (probably not)) TS does not support the satisfies keyword with function
It does, you are just missing parentheses

const myRouteResolveThing = (({ queryParamMap }, _, someService = inject(SomeService)) => {
  // ...
  return 123;
}) satisfies ResolveFn<number>;

https://www.typescriptlang.org/play?#code/C4TwDgpgBAggxsAlgNwIbAgEwEoHsCuGAygHapgDOAFrsFALxQDeAjvhAE4gAKqHqAWwCy5AFzMAvhIBQoSFDyFORYOgily1Wg0mzw0bBAq4ANsggAxEgB4AKgD4dACg4EM4+EjQYcb9WUoaYAAaKApVdwU-DhU1DUDaAEoGR25XAUQKCDtHAB8oWwBuaQAzfBIERFwSKEQSACsIBCdUcVQSEESmGTgTVAoKKCJcAXVOZEQ4CG7paThq8KgBEEUMQ2MzCFsqOoBzZycmKDZOHj5BETAoCVCAfVDjUaJxyehGOsbm4aeXqcTk+iOJjSKBQAD0YKgADoYSCoBwIMB8BwagBGABMAGZihJkhR0JkSogjAojKZzFZrCR8AIAEaceyFIA

low sluice
#

vscode (and a bunch of opened issues on the ts github) disagree though

#

I think prettier also choked on it

#

First thing I tried

topaz lava
#

you sure? The first thing I found is this issue https://github.com/microsoft/TypeScript/issues/51556 which states in the description

satisfies is super handy to use, and you can already use it for arrow functions.
So the issue just seems to be that you can't use it directly on function declarations (like function myRouteResolveThing....)

GitHub

Suggestion 🔍 Search Terms satisfies function ✅ Viability Checklist This wouldn't be a breaking change in existing TypeScript/JavaScript code This wouldn't change the runtime behavior of exi...

low sluice
#

Probably need to bump prettier, not sure why Nx is still on 2.6 for a new project