'Cannot find namespace 'xxx'.ts(2503)

apis/link.ts:

export interface Link {
  id: string;
  text: string;
}

export function getLinks(): Link[] {
  return [{ id: '1', text: 'a' }];
}

apis/index.ts:

import * as link from './link';

export default {
  link,
};

main.ts:

import apis from './apis';

type Data = apis.link.Link;  // Cannot find namespace 'apis'.ts(2503)

function main() {
  const data: Data[] = apis.link.getLinks();
  console.log(data);
}

I want to use apis.link as some kind of namespace. When I try to import the apis module and use the Link interface defined in apis/link.ts file, TSC throws an error:

Cannot find namespace 'apis'.ts(2503)

tsconfig.json:

{
  "compilerOptions": {
    "target": "es5",
    "module": "commonjs",
    "lib": ["es2015", "es2016", "dom", "es2018.promise"],
    "noImplicitAny": false,
    "strictNullChecks": true,
    "strictFunctionTypes": true,
    "strictPropertyInitialization": true,
    "noImplicitThis": true,
    "alwaysStrict": true,
    "esModuleInterop": true
  }
}

TypeScript version: "typescript": "^4.1.3"



Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source