tannerntannern / ts-mixer

A small TypeScript library that provides tolerable Mixin functionality.
MIT License
379 stars 27 forks source link

Argument of type 'Function' is not assignable to parameter of type 'PropertyDecorator | MethodDecorator | ClassDecorator'. #23

Closed moltar closed 4 years ago

moltar commented 4 years ago

Getting this with TypeORM decorator:

import { Entity, CreateDateColumn } from 'typeorm'
import { decorate } from 'ts-mixer'

@Entity()
export class CreatedAt {
  @decorate(CreateDateColumn())
  createdAt!: Date
}
(alias) CreateDateColumn(options?: ColumnOptions | undefined): Function
import CreateDateColumn
This column will store a creation date of the inserted object. Creation date is generated and inserted only once, at the first time when you create an object, the value is inserted into the table, and is never touched again.

Argument of type 'Function' is not assignable to parameter of type 'PropertyDecorator | MethodDecorator | ClassDecorator'.
  Type 'Function' is not assignable to type 'ClassDecorator'.
    Type 'Function' provides no match for the signature '<TFunction extends Function>(target: TFunction): void | TFunction'.ts(2345)
Screen-Shot-2020-04-20-13-58-49

Any ideas?

Thank you!

tannerntannern commented 4 years ago

Thanks for raising the issue @moltar. The problem seems to be with the type definition of CreateDateColumn. The return type is set as Function, but it really should be PropertyDecorator if the authors of TypeORM wanted to be as specific as possible. I would raise an issue with them if you want to fix the root of the problem, but if you need something to work around the issue right away, you have a few options:

  1. @decorate(CreateDateColumn() as PropertyDecorator) which would be the correct typing
  2. @decorate(CreateDateColumn() as any) if you don't care about typing in this scenario
  3. If you happen to be using the TS 3.9 beta, you could try using the new ts-expect-error feature, which will behaves similarly to @ts-ignore but will cause a compiler error when there isn't an error being suppressed. That way you could open the issue with TypeORM (which may take some time to be resolved) and if/when they address the issue, you'll get compiler errors notifying you of the unnecessary @ts-expect-error comments, and you will be able to get rid of them.
    // @ts-expect-error
    @decorate(CreateDateColumn())

Another option that I didn't mention is that I could simply relax the typing, but I'd really prefer not to go that route. I think it's almost always better to have stricter typing when possible.

Let me know if there's anything else I can help with!

moltar commented 4 years ago

Thanks for the explanation, I will open an issue at TypeORM repo.

I will close this issue, as I guess there is nothing actionable here at the moment.

Again, thank you for your detailed response.