1

When using THREE.js along with Typescript, you can use the same names for types and code. For example:

import * as THREE from '/build/three.module.js'

// Following line uses THREE.Scene as type and code
const scene: THREE.Scene = new THREE.Scene()

Demo

Im trying to replicate the same behaviour in my own code, but In not sure how to do it. This is what Ive tried:

// typedModule.ts

export const typedConstant1: string = "string";

// Doesnt work
// declare global {
//     namespace typedModule {
//       export type typedConstant1 = typeof typedConstant1
//     }
//   }

// index.ts

import * as typedModule from "./typedModule";

const someVariable: typedModule.typedConstant1 = typedModule.typedConstant1; // ERROR: Namespace '"/sandbox/src/typedModule"' has no exported member 'typedConstant1'.ts(2694)

Demo

0

1 Answer 1

3

You should be able to export types the same way you export values:

export const typedConstant1: string = "string";
export type typedConstant1 = typeof typedConstant1;

and then importing should work:

import * as typedModule from "./typedModule";

const someVariable: typedModule.typedConstant1 = typedModule.typedConstant1;
// const someVariable: string

Modified codesandbox demo

Similar code in TS Playground

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.