[Solved] absolute paths with Expo and TypeScript

I’m using TypeScript and was looking for way to import using absolute path. The solution in this forum did not work for me but I could not comment there as the topic was locked. I found a solution somewhere else and would like to share it.

I put all my components in the app/ folder. Below is the structure.

...
app/
  |-- package.json
  |...
App.tsx
tsconfig.json
babel.config.js
...

I need to add package.json file in app/ folder.

{
  "name": "app"
}

I modified tsconfig.json. Add baseUrl and paths

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

babel.config.js

module.exports = function (api) {
  api.cache(true);
  return {
    presets: ['babel-preset-expo'],
    plugins: [
      [
        'module-resolver',
        {
          alias: {
            app: './app',
          },
        },
      ],
    ],
  };
};

Now I can

import SomeComponent from 'app/subfolder/SomeComponent'

<Image
      source={require('app/assets/images/home.jpg')}
      resizeMode="cover"
/>

Hope it helps.