r/angular • • 1d ago

Have I Solved A Problem That Doesn't Exist?

From my anecdotal experience, and stuff I've read here and elsewhere, type-checking with Zod isn't really used by Angular devs. Obviously the compiler will pick up TS definition errors, but I've been the victim of APIs, internal and 3rd party, changing without warning or by mistake and causing issues in the build.

This isn't an issue if you're TS back-to-front and are sharing your interfaces and types between them, or if you're dynamically generating your typescript definitions from the backend (though that could lead to surprises too). Mainly, just if you're in a frontend team a bit detached from the backend.

I'd appreciate some feedback on this: https://www.npmjs.com/package/@ribaldry/ngx-type-drift

The idea is that problems show up while you develop, nothing additional is added to the build only in serve, and you can wire-up your own alerting (toast, snackbar) if you want. Example: BE has been serving an object with property car_type and have now changed it to carType - you're running against a UAT or Test environment so you pick it up quick. Your tests will probably pick it up, hopefully this catches it sooner.

Complete waste of time?

EDIT:

Thanks for the feedback - never a waste of time to interact with nice Angular folks, but this does seem to be solving a problem that would be better solved elsewhere in the build process. Appreciate all those who responded!

0 Upvotes

14 comments sorted by

1

u/GregorDeLaMuerte 1d ago

Complete waste of time?

Absolutely not. I like it for the pragmatic approach.

However if I understand correctly it only catches things you're currently developing on. Say your application uses 100 different API endpoints and the feature you're currently working on only uses 3 of them. You'd be able to detect breaking changes for the 3 while you're clicktesting, but the rest is uncovered.

1

u/ItIsForMyArmpits 1d ago

Yep, that right - unless the ws or http is intercepted it's not going to flag it. If your e2e and unit tests are all mocked it will miss it there too. Definitely a flaw.

1

u/ahaw_work 1d ago

I'm sure this could have some use cases but I would rather create integration test against BE or the external service. In this case we would not wait for the error to occur in runtime but detect it proactively. It also wouldn't need clicktesting (yes, you could automate it but if you would that it's better to do integration tests)

1

u/ItIsForMyArmpits 1d ago

Some sort of batch file that can make a call against declared APIs as part of the build step, maybe?

1

u/Bjeaurn 1d ago

I don't think runtime testing for this is the way to go. This is what compilers and (daily/nightly) pipelines are for. If they contain contract tests great. If the APIs are external that sounds like an integration test.

2

u/ItIsForMyArmpits 1d ago

Definitely a good point, I'm just hoping to catch these before that stage - BE devs working on an API maybe failing to communicate a key change, you don't want to wait until they release that change to find out its broken something - certainly harder to catch until that stage with 3rd party APIs but then they tend to be more... communicative about their changes.

1

u/Bjeaurn 1d ago

I get that but that's what contract tests are for, they affect BE too.

Waiting until an error in runtime is much too late for that feedback loop if you ask me.

1

u/ItIsForMyArmpits 9h ago

Sorry, I was agreeing with your point. I guess what I'm trying to do is maybe automate contact tests from/using our existing TS definitions.

1

u/Equivalent_Style4790 18h ago

I came from a c# and java background so i use classes and constructors. I create my object with new and pass in the json i receive from the api, so my classes are perfectly typed, u handle the mess in my constructor, and i banned the use of “as”.

1

u/ItIsForMyArmpits 9h ago

I could be wrong, but typescript is syntactic sugar on JavaScript. At compile time it'll pick up type errors but unless you introduce a type checking library like zod into your client side code, type differences won't be picked up at runtime.

1

u/Equivalent_Style4790 6h ago

I manually check in the constructor. In the end of my constructor all the class field types are checked to be correct. The constructor do the conversion and/or add the default values. When u use pure ts objects, u are safe

1

u/Discordbothost 1d ago

Clarify that shared TS types don't rule this out. They don't validate the actual HTTP response, and frontend/backend deployments can get out of sync. Documenting which schema version the checker expects would make its value clearer even for teams using TS on both sides.

1

u/ochreui 7h ago

That part is doable without writing schemas by hand. ts-json-schema-generator can turn the interfaces you already have into JSON Schema, and a small script in the nightly pipeline can call each GET endpoint on the test environment and validate the responses with ajv. That covers all 100 endpoints, not only the 3 you happened to click through, and the backend team hears about a renamed field the morning after it lands.

Your package could stay as the dev-time half of that: same schemas, checked in an interceptor that only runs under ng serve.