Alias import

2 snippets across 2 stacks — Python, TypeScript

Also written as import alias

PYPython

Basic Imports

PY · Modules & Imports
syntax
import module
from module import name
import module as alias
example
import json
from pathlib import Path
from collections import defaultdict, Counter
import numpy as np  # common alias convention

data = json.dumps({"key": "value"})
print(data)
output
{"key": "value"}

Note Convention: standard library imports first, then third-party, then local. Separate groups with a blank line. Use isort to auto-format.

TSTypeScript

paths & baseUrl

TS · Configuration
syntax
{
  "compilerOptions": {
    "baseUrl": ".",
    "paths": {
      "@alias/*": ["src/folder/*"]
    }
  }
}
example
// tsconfig.json
{
  "compilerOptions": {
    "baseUrl": ".",
    "paths": {
      "@components/*": ["src/components/*"],
      "@utils/*": ["src/utils/*"],
      "@api/*": ["src/api/*"],
      "@types/*": ["src/types/*"]
    }
  }
}

// In your code:
import { Button } from "@components/Button";
import { formatDate } from "@utils/date";
import type { User } from "@types/user";
output
// Path aliases shorten deep relative imports like ../../../../utils

Note paths only affects TypeScript's type resolution — it does NOT rewrite imports in emitted JS. Your bundler (Vite, webpack) or runtime (ts-node, tsx) needs matching alias config. For Node.js, package.json "imports" with subpath patterns is the modern alternative that works without bundler config.