TypeStrong / ts-node

TypeScript execution and REPL for node.js
https://typestrong.org/ts-node
MIT License
12.9k stars 535 forks source link

error TS2503: Cannot find namespace (tsc compiled, ts-node failed) #836

Closed kikar closed 5 years ago

kikar commented 5 years ago

Node

10.15.3

Npm

6.9.1-next.0

Typescript

3.4.5

ts-node

8.1.0

tsconfig.json

{
  "compilerOptions": {
    "module": "commonjs",
    "target": "es2017",
    "emitDecoratorMetadata": true,
    "experimentalDecorators": true,
    "suppressImplicitAnyIndexErrors": true,
    "noImplicitAny": true,
    "skipLibCheck": true,
    "strictFunctionTypes": true,
    "strictPropertyInitialization": false,
    "strictNullChecks": false,
    "moduleResolution": "node",
    "sourceMap": true,
    "outDir": "dist",
    "removeComments": true,
    "baseUrl": ".",
    "paths": {
      "*": ["node_modules/*"]
    }
  },
  "include": ["src/**/*"],
  "exclude": ["src/**/*.test.ts"]
}

Compiling gives me an error: error TS2503: Cannot find namespace 'Crud'.

File crud.types.ts

namespace Crud {
  export interface Foo {}
}

File using Crud, no import needed (is in the name directory)

const options: Crud.Foo = ...
blakeembrey commented 5 years ago

There’s a section in the README called “I can’t find types” - have you checked that?

kikar commented 5 years ago

Totally missed that, thank you!

Maxim-Mazurok commented 4 years ago

There’s a section in the README called “I can’t find types” - have you checked that?

I'm experiencing similar problem, but can't find that section in the README anymore. Can you tell me, please, where is it now?

lotar94 commented 4 years ago

i am having the same problem. In all namespace it is give me error => error TS2503: Cannot find namespace. I'm updating from angular 7 to 9

this is my package.json dependencies "dependencies": { "@agm/core": "1.0.0", "@agm/js-marker-clusterer": "1.0.0", "@angular/animations": "^9.1.9", "@angular/cdk": "^7.3.7", "@angular/common": "^9.1.9", "@angular/compiler": "^9.1.9", "@angular/core": "^9.1.9", "@angular/forms": "^9.1.9", "@angular/platform-browser": "^9.1.9", "@angular/platform-browser-dynamic": "^9.1.9", "@angular/router": "^9.1.9", "@fortawesome/fontawesome-free": "^5.12.1", "@ngx-translate/core": "^11.0.1", "@ngx-translate/http-loader": "^4.0.0", "@types/googlemaps": "^3.39.2", "bootstrap": "^4.4.1", "chart.js": "^2.9.3", "core-js": "^2.6.11", "js-marker-clusterer": "^1.0.0", "ngx-clipboard": "^12.3.1", "ol": "^5.3.3", "primeflex": "^1.0.0", "primeicons": "^1.0.0", "primeng": "^8.0.0", "quill": "^1.3.7", "rxjs": "~6.5.5", "terser": "^3.17.0", "tslib": "^1.10.0", "xlsx": "^0.14.3", "zone.js": "~0.10.2" }

"devDependencies": { "@angular-devkit/build-angular": "~0.901.7", "@angular/cli": "^9.1.7", "@angular/compiler-cli": "^9.1.9", "@angular/language-service": "^9.1.9", "@commitlint/cli": "^8.3.5", "@commitlint/config-conventional": "^7.6.0", "@types/jasmine": "^3.5.3", "@types/jasminewd2": "^2.0.8", "@types/node": "^12.11.1", "codelyzer": "^5.1.2", "cz-conventional-changelog": "^2.1.0", "husky": "^1.3.1", "jasmine-core": "~3.3.0", "jasmine-spec-reporter": "~4.2.1", "karma": "^4.4.1", "karma-chrome-launcher": "~2.2.0", "karma-coverage-istanbul-reporter": "^2.1.1", "karma-jasmine": "~2.0.1", "karma-jasmine-html-reporter": "^1.5.2", "lint-staged": "^8.1.7", "nyc": "^13.3.0", "protractor": "^5.4.3", "ts-node": "^8.6.2", "tslint": "~5.12.1", "typescript": "~3.8.3" }

and this is my tsconfig.json

{ "compileOnSave": false, "compilerOptions": { "baseUrl": "./", "downlevelIteration": true, "outDir": "./dist/out-tsc", "sourceMap": true, "declaration": false, "module": "esnext", "moduleResolution": "node", "emitDecoratorMetadata": true, "experimentalDecorators": true, "target": "es2015", "typeRoots": [ "node_modules/@types" ], "lib": [ "es2018", "dom" ] } }

and this is my tsconfig.app.json { "extends": "../tsconfig.json", "compilerOptions": { "outDir": "../out-tsc/app", "types": [] }, "files": [ "main.ts", "polyfills.ts" ], "include": [ "src/*/.d.ts" ] }

someone can help me.

Maxim-Mazurok commented 4 years ago

Remove this: "types": []

lotar94 commented 4 years ago

@Maxim-Mazurok still it's happen, i removed that but still happend

cpimenta-apsoftware commented 3 years ago

@lotar94 try renaming the crud.types.ts file to crud.types.d.ts, this worked for me

zimeg commented 2 years ago

@Maxim-Mazurok the section header might be named "Missing Types" now

awais2080 commented 2 months ago

i am having the same issue can not find namesapce 'Business'

i have 2 libraries which are declaration-library and function-library

declaration-library i am using this library for types, interfaces and namespaces

tsconfig.lib.json


/* To learn more about Typescript configuration file: https://www.typescriptlang.org/docs/handbook/tsconfig-json.html. */
/* To learn more about Angular compiler options: https://angular.dev/reference/configs/angular-compiler-options. */
{
  "extends": "../../tsconfig.json",
  "compilerOptions": {
    "outDir": "../../out-tsc/lib",
    "declaration": true,
    "declarationMap": true,
    "inlineSources": true,
    "types": []
  },
  "exclude": [
    "**/*.spec.ts"
  ]
}

let say i have sample file field.ts file in my declaration library angular 18

// related: https://alex-klaus.com/stongly-typed-angular-forms/

declare namespace Business {
  export interface EditField extends Business.FormControl {
    isDirty: boolean;
    isRequired: boolean;
    isBeingProcessed: boolean;
    isRecentlyProcessedWithSuccess: boolean;
    isRecentlyProcessedWithError: boolean;
    valueType: Business.ValueType;
    parameterName?: string;

    setUserValueToFieldValue(): void;
    toValueType(x: any): any;
    hasValue(): boolean;

    validationOptions: Business.FieldValidationOptions;
  }
}

i have module which import this file like below.

/// <reference path="./interfaces/field/field.ts" />

and module is linked to public-api.ts

let say it is build successfully.

Now i have function-library where i am using this Business.EditField


import { UntypedFormControl, ValidatorFn, Validators } from '@angular/forms';

import { of, Observable } from 'rxjs';
import { catchError, mergeMap } from 'rxjs/operators';

/*
  - EditField extends FormControl to:
    + provide more control with validation and input restriction
    + add api update calls
  - See /demos/fields/text for some good examples
  - See also: https://github.com/angular/angular/issues/13461
*/
export class EditField
  extends UntypedFormControl
  implements Business.EditField
{
  isDirty: boolean;
  isRequired: boolean;
  isBeingProcessed: boolean;
  isRecentlyProcessedWithSuccess: boolean;
  isRecentlyProcessedWithError: boolean;
  restrictions: Business.FieldRestrictions = {
    keyboardEventIsAllowed: function (keyboardEvent: KeyboardEvent) {
      return true;
    },
  };

  list: Business.ListItem[];

  constructor(
    public apiCaller: (val: any) => Observable<any>,
    public valueType: Business.ValueType,
    formState: any = null,
    public validationOptions: Business.FieldValidationOptions = {},
    public restrictionOptions: Business.FieldRestrictionOption[] = [],
    public formattingOption?: Business.FieldFormattingOption,
    public parameterName?: string
  ) {
    super(formState);
    this.setValidations(validationOptions);
    this.setRestrictions(restrictionOptions);
  }

i have already attached the path to my tsconfig.lib.json in function-library


/* To learn more about Typescript configuration file: https://www.typescriptlang.org/docs/handbook/tsconfig-json.html. */
/* To learn more about Angular compiler options: https://angular.dev/reference/configs/angular-compiler-options. */
{
  "extends": "../../tsconfig.json",
  "compilerOptions": {
    "outDir": "../../out-tsc/lib",
    "target": "ES2022",
    "declaration": true,
    "declarationMap": true,
    "inlineSources": true,
    "types": ["../../dist/declaration-library"],
    "lib": ["dom", "ES2022"]

  },
  "angularCompilerOptions": {
    "skipTemplateCodegen": true,
    "strictMetadataEmit": true,
    "enableResourceInlining": true
  },
  "include": ["src/**/*.ts"],
  "exclude": ["src/test.ts", "**/*.spec.ts"]
}

i have been trying to resolve it but no success so far. Any help would be really appreciated and Thanks in Advance :)