javascript - ESLint complains TypeScript type declaration uses type before it was declared - Stack Overflow

I am creating a builder that has types, the way I declare the type is:export type BuilderInterface<T

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 plains 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?

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 plains 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?

Share Improve this question asked Nov 16, 2018 at 13:28 SirPeopleSirPeople 4,34829 silver badges47 bronze badges
Add a ment  | 

2 Answers 2

Reset to default 4

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!

It's because types unlike interfaces are not self-referentiable. You should either depose 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./Microsoft/TypeScript/issues/3496#issuement-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.

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1745140554a4613410.html

相关推荐

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信