How can I separate my typescript code into multiple files?

original.roland

Previously I'm mostly experienced in C++ development, where including another file is pretty easy. In typescript it doesn't seem to be so much trivial.

There are a few huge arrays, which I'd like to declare only once as const and then use them later in my core logic. Because of the size of those arrays I wouldn't like to pollute my main code with it, I'd like to put it into an external ts file. I've tried to google and include every single possibility, but it seems I can't get it right.

My tsconfig.json:

{
    "compilerOptions": {
        "module": "commonjs",
        "target": "es6",
        "outDir": "out",
        "lib": [
            "es6"
        ],
        "sourceMap": true,
        "rootDir": "src",
        "strict": true,   /* enable all strict type-checking options */
        /* Additional Checks */
        "noImplicitReturns": true, /* Report error when not all code paths in function return a value. */
        "noFallthroughCasesInSwitch": true, /* Report errors for fallthrough cases in switch statement. */
        // "noUnusedParameters": true,  /* Report errors on unused parameters. */
    },
    "include": [
        "src/*"
    ],
    "exclude": [
        "node_modules",
        ".vscode-test"
    ],
    "files": [
        "src/extension.ts",
        "src/templates.ts",
        "src/intellisense.ts",
    ]
}

Main code is the extension.ts, the other two should only contain some constant variables. Sample code from intellisense.ts:

import * as vscode from 'vscode';

const completionTypes = [
    new vscode.CompletionItem('text1'),
    new vscode.CompletionItem('text2'),
    new vscode.CompletionItem('text3'),
    ... // A hundred more entry
    ];

But when I try to use that completionTypes in my code in extension.ts the linter says Cannot find name 'completionTypes'.ts(2304)

How is it possible to organize my code in typescript?

Sam Herrmann

You need to export the completionTypes to make it accessible to other files:

intellisense.ts:

import * as vscode from 'vscode';

export const completionTypes = [
  // ...
];

extension.ts:

import { completionTypes } from './intellisense';

//...

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

In VS Code, how can I break my project into multiple files?

How can I separate multiple graphs in this code?

How can i separate my networking code from my ViewController

How do I organize my Processing code in separate files?

How can I split my Ada program into separate files

How can I split my koa routes into separate files?

How can I separate runs of my TensorFlow code in TensorBoard?

How can I make a separate class for a fire ball in my code?

How can i separate php code with html to have them both clean and separate in different files?

How do I split my QML code into multiple files?

How can I split my linux file into multiple files?

How can I separate my content on powershell?

How can i run multiple queries on my code in php?

How can I create Multiple selected for my recyclerview code?

How can I process multiple dataframes and save the output in separate files using R?

How can I access my Express API with my Typescript NextJS code?

How do I deal with d.ts files when publishing my typescript code to npm

How can I get all my itunes files from multiple computers onto my new PC?

How can I have my code run through different excel files and export unique files of results in R?

How do I break up my PyGame game into separate files?

How can I merge multiple arrays into multiple separate objects?

How do I save multiple histograms to different (separate) files?

How can I merge typescript interfaces over multiple files in the same namespace

How can I separate many files from one folder into separate folders based on the name of the files?

How can I separate multiple files from single file caused by zstd -r folder -o output.zst?

How can I combine multiple lines of text into one line in Python with a delimiter to separate them with big files (4gb+)

How can I write dplyr groups to separate files?

How can I print lines from a file to separate files

How can I split a CA certificate bundle into separate files?