Edoardo Trotta
Edoardo Trotta

Reputation: 117

Typescript path not working in tsconfig.json

I have this tsconfig.json file with paths that I need to resolve in a certain way.

{
    "compilerOptions": {
        "allowSyntheticDefaultImports": true,
        "jsx": "react-native",
        "lib": ["dom", "esnext"],
        "moduleResolution": "node",
        "noEmit": true,
        "skipLibCheck": true,
        "resolveJsonModule": true,
        "strict": true,
        "noImplicitAny": false,
        "baseUrl": "./",
        "paths": {
            "components/*": ["./app/components"],
            "components/*/*": ["./app/components/*/index"], // This line does not work
            "services/*": ["./app/services"],
            "app/*": ["./app"],
            "navigation/*": ["./app/navigation"],
            "theme/*": ["./app/theme/*"]
        }
    }
}

The highlighted line is throwing this error:

Pattern 'components/*/*' can have at most one '*' character.

I tried every possible solution and I cannot find any single solution whatsoever. It seems impossible to even find the same error message somewhere on the internet. Any helpful suggestions would be massively appreciated.

Upvotes: 0

Views: 1657

Answers (1)

Jethro91
Jethro91

Reputation: 565

Development

npm i -save-dev tsconfig-paths/register

tsconfig.json

{
 "compilerOptions": {
    "baseUrl": ".",
    "paths": {
      "@src/*": ["src/*"],
    },
  }
}

package.json

"scripts": {
  dev: "ts-node -r tsconfig-paths/register src/index.ts"
}

Build

npm i -save-d ttypescript @zerollup/ts-transform-paths

tsconfig.json

{
 "compilerOptions": {
    "baseUrl": ".",
    "paths": {
      "@src/*": ["src/*"],
    },
  }
 "plugins": [
      {
          "transform": "@zerollup/ts-transform-paths",
      }
  ],
}

package.json

"scripts": {
  build: "ttsc -P ./tsconfig.json"
}

Upvotes: 1

Related Questions