❤️
Web/mobile developer at BAM
*.d.ts
*.test-d.ts
*.ts
or the full URL (if the one above doesn't work): https://github.com/pierpo/journey-through-typing-loopback
git checkout -f step2-expectedor the full URL (if the one above doesn't work): https://github.com/pierpo/journey-through-typing-loopback
git clone git@github.com:pierpo/journey-through-typing-loopback.git
cd journey-through-typing-loopback
yarn
git checkout step1-expected
# Now let's run the tests!
yarn testor the full URL (if the one above doesn't work): https://github.com/pierpo/journey-through-typing-loopback
do not touch
touch
Legend:
src
typings
index.ts
index.d.ts
index.test-d.ts
type definitions
type tests
empty source
Don't think too much about it, it's quite dumb
Not the whole API, only the scope of our exercise
const userEntity = {
name: 'john',
age: 20,
};Properties:
const userInstance = app.models.User.findOne();
userInstance.update({ age: 20 })Properties:
const userInstance = app.models.User.findOne();
userInstance.update({ age: 20 })Properties:
Note: this is not rigorously a UML...
Abstract classes were not included in the diagram, we'll see this through the exercise ;)
UserEntity (for now)
Let's use generics
You may use the Partial<T> utility type.
type A = { mykey: string };
type B = Partial<A>; // same as A, but all keys become optionalPartial<T> won't be enough.
You will need mapped types!
const SimpleQuery = {
age: 20;
} // this is a partial of User indeed
const NestedQuery = {
age: { neq: 20 }
} // but this is not a partial anymore
// in User, the value under age is not of type { neq: number }type B = {
[K in keyof A]: A[K];
} // this is equivalent to A
type C = {
[K in keyof A]?: { something: A[K] };
} // but you can rework the type shape!Allow only keys that are present in the entity
Allow only keys that are a relation in the entity
They're meant to attach API routes to models
❤️