SirPeople
SirPeople

Reputation: 4348

ESLint complains TypeScript type declaration uses type before it was declared

I am creating a builder that has types, the way I declare the type is:

export type BuilderInterface<T> = {
  [key in keyof T]: (arg: T[key]) => BuilderInterface<T> } & {
  build(): T
}

When running ESLint it complains saying that: "BuilderInteface" was used before it was defined (no-use-before-define). What is normal since I need to assert that each argument functions returns a builder of the same kind.

Which ways do I have to declare this without breaking the eslint rule? Should I ignore the rule directly? Why?

Upvotes: 4

Views: 3540

Answers (2)

Fenton
Fenton

Reputation: 250922

ESLint is (kind of) correct, because technically you haven't declared the type and you're using it. Recursive types are quite hard to handle. Try using TSLint to see if you get a better result as it understands TypeScript better.

The TypeScript team are pretty good at recursive types, so it's a valid type.

Disable the rule or create an exception so the tools let you get on with your job!

Upvotes: 4

Nurbol Alpysbayev
Nurbol Alpysbayev

Reputation: 21881

It's because types unlike interfaces are not self-referentiable. You should either decompose your type to avoid self-referencing or use interfaces.

P.S. The above is in theory... For some reason though, your type works for me (no tslint, just ts). I guess new versions of TS handle this well. Check this: https://github.com/Microsoft/TypeScript/issues/3496#issuecomment-128553540

P.P.S. I just noticed you were talking about JSLint, not TSLint. JSLint of course is not a good tool to check TS code. Give TSLint a try, instead.

Upvotes: 2

Related Questions