1

I'm reading the loopback 4 documentation. I also already read typescript tutorials. But I don't get this syntax:

module.exports = <ModelCrudRestApiConfig>{
  model: Product,
  pattern: 'CrudRest', // make sure to use this pattern
  dataSource: 'db',
  basePath: '/products',
};

What does this notation mean:

<Stuff>{ a: 1, c:2 }

Also this

const ProductController = defineCrudRestController<
      Product,
      typeof Product.prototype.id,
      'id'
    >(Product, {basePath: '/products'});

From here: https://loopback.io/doc/en/lb4/Creating-crud-rest-apis.html

Also, is it from loopback or from typescript?

Thanks,

Mon
  • 1,010
  • 1
  • 11
  • 19

1 Answers1

2

That's Typescript type assertion (in other words you telling the compiler that you know about the types better than it does).

Basically this things are the same, just different syntax:

const stuff = <Stuff>{ a: 1, c:2 };

const stuff = { a: 1, c:2 } as Stuff;

More info here: https://basarat.gitbook.io/typescript/type-system/type-assertion

Danila
  • 15,606
  • 2
  • 35
  • 67
  • Wow thank you very much for the quick response. Sorry is this also typescript? `defineCrudRestController< Product, typeof Product.prototype.id, 'id' >(Product, {basePath: '/products'});`. I thought I read enough. It looks like a generic combined with a function but I didn't see anything like that. Also the generics don't have multiple inputs/params inside angled brackets – Mon Aug 31 '20 at 11:47
  • 1
    Yes, this is `Generic` syntax, it uses similar syntax as assertion but it is a different thing. It is a little bit harder topic if you are not very familiar with typed languages. In some way generics are similar to function arguments, you usually pass them to functions or classes (or even other types). You can read more about it here https://basarat.gitbook.io/typescript/type-system/generics. – Danila Aug 31 '20 at 11:51