GithubHelp home page GithubHelp logo

TS incorrectly flags a side effect-only import as TS5097 (An import path can only end with a '.ts' extension...) in `--watch` mode about typescript HOT 8 CLOSED

Aarononomous avatar Aarononomous commented on July 25, 2024
TS incorrectly flags a side effect-only import as TS5097 (An import path can only end with a '.ts' extension...) in `--watch` mode

from typescript.

Comments (8)

Andarist avatar Andarist commented on July 25, 2024 1

I didn't say I'm happy I fixed this, did I? :trollface:

from typescript.

Andarist avatar Andarist commented on July 25, 2024 1

At that point, couldn't you import './a.base'? There would be no a.base.platformX.ts so it would eventually resolve to just a.base.ts.

from typescript.

Andarist avatar Andarist commented on July 25, 2024

accidentally-ish one of my recent PRs fixes this: #58668 , maybe @jakebailey could create a TS build of that PR so you could fully confirm that

from typescript.

fatcerberus avatar fatcerberus commented on July 25, 2024

@Andarist And you wanted to make this an error across the board... tsk tsk 😉

from typescript.

Andarist avatar Andarist commented on July 25, 2024

I really wonder why @Aarononomous is doing this in his code though. What's the reason behind importing a side-effectful .ts without enabling allowImportingTsExtensions? 🤔

from typescript.

Aarononomous avatar Aarononomous commented on July 25, 2024

I really wonder why @Aarononomous is doing this in his code though. What's the reason behind importing a side-effectful .ts without enabling allowImportingTsExtensions? 🤔

I'm working in a multitenant codebase. We put platform-specific code in *.platformName.ts files, adding the platform name as an extension. Then we have a webpack resolver configured to look for platform files preferentially,

resolve: {
  extensions: [
    ...
    `.${CONFIG.platform}.ts`,
    `.${CONFIG.platform}.tsx`,
    '.ts',
    '.tsx',
    ...

so that when we build for, say, platformX and see import './a', we first look for a file at ./a.platformX.ts, then ./a.ts. This allows us to write all of our platform-specific code without runtime tests or compiler macros or whatever, neatly encapsulated in its own file.

So not only do we not want to allow ts extensions, we can't! Otherwise our whole multitenant resolver behavior falls apart.

The exception to this is in the multitenant files themselves, where we do sometimes want to import common code from the extensionless base files, e.g.

a.platformX.ts

// @ts-expect-error
// We need the full filename with extension here: otherwise there's a circular import
import { module as baseModule } from './a.ts';

// do something with baseModule...

export const module;

The 'allowImportingTsExtensions' error is expected when we import with a full filename. It's just when we only import for side effects from the base file that we see this inconsistency in --watch mode.

So the code example above is really more like

a.ts

window.__CONFIG__ = { ... };

a.platformX.ts

import './a.ts';

window.__CONFIG__.platformX = { ... };

from typescript.

Andarist avatar Andarist commented on July 25, 2024

The use case makes sense but I think you could introduce some other convention for those base modules to avoid this problem altogether.

from typescript.

Aarononomous avatar Aarononomous commented on July 25, 2024

@Andarist, what do you suggest?

If I put my side-effectful code in, say, a.base.ts, I'd still be calling it in a.ts, a.platformX.ts, etc. with

import 'a.base.ts';

from typescript.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.