Skip to content

Option to respect closest tsconfig.json of imported files #37853

Closed
@alshdavid

Description

@alshdavid

Search Terms

use nearest tsconfig.json for imported packages

Suggestion

When importing source files which have source that is expected to be compiled using a different tsconfig.json, the compiler will throw errors if the imported source has code which conflicts with the compiler options which the compiler was initialized with.

eg:

/consumer
   index.ts
   tsconfig.json
/target
   index.ts
   tsconfig.json

consumer imports target. consumer has strict: true while the target has strict: false

Add an option to the compilerOptions such as useNearestTSConfigInImports: boolean.

This will help to avoid the necessity of the complex build systems, symlinks and project structures we see in modern multi-package TypeScript projects.

Use Cases

When working with projects that contain multiple services, it's beneficial to abstract and share dependencies which are used across the multiple consumers.

While TypeScript source can be imported from folders arbitrarily, this assumes the source conforms to the configuration in the compilerOptions of the current context.

As soon as the source needs to be shared between consumers developers must:

  • Extract the source
  • Configure a new compiler
  • Compile the source to JavaScript
    • it's important to mention that this is required even if the package isn't being published to npm
  • Symlinks created to that package
    • This can get quite complicated if there are post build steps, such as moving folders, etc.

Examples where that might be the case:

  • Integrating older, less safe code which doesn't have strict: true yet
  • Integrating source which contains uncommon configuration (eg strictBindCallApply)
  • Consumer services that use multiple frameworks (react/preact)

This complexity is introduced pretty quickly in projects and would be easily mitigated by allowing the TypeScript compiler the capacity to use the nearest tsconfig as the source of truth for the files nested in the folders below it.

It enable projects written in TypeScript with no consideration of JavaScript until the service was to be published. This results in very clean and easy to traverse repositories.

Something like this is currently possible using comments, eg: adding /** @jsx y */ will override the jsx pragma in the current file.

Examples

Here I have an example repository which breaks, but demonstrates the feature proposal.
https://github.com/alshdavid-sandbox/respect-nested-ts-config

Checklist

My suggestion meets these guidelines:

  • This wouldn't be a breaking change in existing TypeScript/JavaScript code
  • This wouldn't change the runtime behavior of existing JavaScript code
  • This could be implemented without emitting different JS based on the types of the expressions
  • This isn't a runtime feature (e.g. library functionality, non-ECMAScript syntax with JavaScript output, etc.)
  • This feature would agree with the rest of TypeScript's Design Goals.

Metadata

Metadata

Assignees

No one assigned

    Labels

    SuggestionAn idea for TypeScriptToo ComplexAn issue which adding support for may be too complex for the value it adds

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions