How to check the type of index signature in .d.ts?

15 views Asked by At

In project-a has a index.d.ts. It contains a interface with index signature like this:

interface FuncOpts {
  foo?: string;
  bar?: number;
  pro?: boolean;
  [key: string]: string | number | boolean;
}

When run tsc index.d.ts, it shows everything is ok.


In project-b, which depends project-a, when run tsc, will get the errors:

../project-a/index.d.ts:2:3 - error TS2411: Property 'foo' of type 'string | undefined' is not assignable to 'string' index type 'string | number | boolean'.

2   foo?: string;
    ~~~

../project-a/index.d.ts:3:3 - error TS2411: Property 'bar' of type 'number | undefined' is not assignable to 'string' index type 'string | number | boolean'.

3   bar?: number;
    ~~~

../project-a/index.d.ts:4:3 - error TS2411: Property 'pro' of type 'boolean | undefined' is not assi
gnable to 'string' index type 'string | number | boolean'.

4   pro?: boolean;
    ~~~


Found 3 errors in the same file, starting at: ../project-a/index.d.ts:2

Want to check the type error in project-a, just liking the errors in project-b.

For recurrence, see the repo: https://github.com/lihs-learning/typescirpt-declear-file-issue-with-index-signature

1

There are 1 answers

0
LiHS On

I found the problem...

Add belows in tsconfig.json in project-a.

{
  "include": ["index.d.ts"]
}

And just use "check-type": "tsc" in package.json in project-a.