Blog
    By Gabriel CA
    4 min read

    Why and how is TypeScript used in enterprise web applications?

    Why and how is TypeScript used in enterprise web applications?

    TypeScript checks the types in JavaScript code before that code runs. This is static type checking: the check takes place before run time.1 Interfaces name the shapes of values and set contracts inside and outside a project.2

    For a large app, plan for both shared types and checks on incoming data. Project references can split code into smaller parts and keep those parts separate.3 Zod lets you parse data against a schema to get a checked, typed result.4

    How do shared types connect frontends and backends?

    TypeScript checks the shape of values, also called structural subtyping or duck typing. An interface names a shape and sets a contract with code outside the project.2

    Consider this hypothetical account app, with separate web and API packages. Share one account interface between them, rather than writing the same contract twice.

    export interface AccountSummary {
      id: string;
      displayName: string;
    }
    

    When code imports from a referenced project, TypeScript loads its output declaration file, .d.ts.3 For this design, make the shared contract package a reference for both app packages.

    How do project references split a large codebase?

    TypeScript supports project references in version 3.0 and newer to split programs into smaller pieces. They can speed up builds and enforce a clear split between code parts. The tsc --build mode works with these references for faster TypeScript builds.3

    The docs advise a solution tsconfig.json with references to all leaf projects. They also advise an empty files array to stop files from being compiled twice.3

    For the hypothetical account app, consider this top-level file to group the projects. Treat it as the shared build plan, not the full setup for each package.

    {
      "files": [],
      "references": [
        { "path": "./packages/contracts" },
        { "path": "./apps/web" },
        { "path": "./apps/api" }
      ]
    }
    

    How should teams check data from the network?

    Zod lets you set a schema, or data rule, and parse data for a checked result.4 Use this check on incoming network data before passing it to app code.

    Zod's safeParse gives back either parsed data or a ZodError, without a try/catch block. Its result type is a discriminated union, which lets you handle each case on its own.4 In your design, give the error case its own branch.

    Zod infers a static type from the schema rules that you define. You can extract that type with z.infer<> for use in your TypeScript code.4 Consider sharing the schema when you want the type to follow the data rules.

    For a hypothetical account-name input, consider this schema and the type drawn from it. Put the parsing step where data enters, not just an interface in shared code.

    import { z } from "zod";
    
    const AccountNameSchema = z.string();
    type AccountName = z.infer<typeof AccountNameSchema>;
    

    A Zod schema with async checks or transforms needs parseAsync instead of parse. The docs show an async string check called with await schema.parseAsync("hello").4

    What does strict checking change for teams?

    The strict flag turns on all checks in TypeScript's strict-mode family. These checks give stronger guarantees that the program is correct. You can still turn off single checks from that family as needed.5

    Agree on strict settings before teams take charge of separate parts of the code. If you turn off a check, record why and set a review date.

    The exclude setting only changes which files the include setting finds. Files it leaves out can still enter through imports, types, references, or the files list.6 Do not rely on this setting to keep one package apart from another.

    What build and editor costs should teams plan for?

    Projects that depend on others use .d.ts files built from those other projects. To avoid false editor errors, check in some build outputs or build after cloning.3 Choose one route and write it into the team's setup guide.

    Since TypeScript version 3.7, VS Code can generate declaration files in memory behind the scenes. This process may reduce false editor errors, but it has performance costs. For very large composite projects, the docs suggest disableSourceOfProjectReferenceRedirect to turn that process off.3

    The declarationMap flag enables cross-project Go to Definition and Rename in supported editors.3 Consider this flag when teams need to trace and edit types across shared packages.

    For busy data-checking paths, Zod's z.compile(schema) clones a schema with a precompiled fast path. Valid inputs use that path; invalid inputs use the normal parser with the same error reports.4 Measure your own workload before choosing to change this part of the design.

    How should you choose the contract design?

    For contracts between code packages, consider shared interfaces and project references as your starting point. For network data, consider shared schemas, inferred types, and parsing where each app receives data.

    Include compiler flags, build outputs, and editor setup in the design review. Ask each team to name its contract owner, input-checking point, and setup needs.

    Sources

    1. typescriptlang.org › intro.html
    2. typescriptlang.org › interfaces.html
    3. typescriptlang.org › project-references.html
    4. github.com
    5. typescriptlang.org › strict.html
    6. typescriptlang.org › tsconfig