question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. ItΒ collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Alias for TypeScript declaration emitting

See original GitHub issue

Search Terms

  • alias
  • declaration

Suggestion

A way to configuration alias for declaration files.

Use Cases

Related: #10866

I understand that TypeScript team want to leaves module resolving for other tools like Webpack. However, most of the tools are focus on emitting JavaScript. If you want to emit TypeScript declaration, you need to use tsc.

This is important when you want to write library with TypeScript that you need to release declaration files.

For example, in Webpack, babel-loader does not emit declarations, ts-loader and awesome-typescript-loader uses tsc.

Alias is useful for you to write simpler import / export statement.

Examples

index.ts

export * from "~/foo";

index.d.ts

export * from "../../foo";

tsconfig.json

{
  "alias": {
    "~/*": ["src/*"]
  }
}

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.

Issue Analytics

  • State:open
  • Created 4 years ago
  • Reactions:70
  • Comments:13 (1 by maintainers)

github_iconTop GitHub Comments

59reactions
joshuaavaloncommented, Apr 17, 2019

@RyanCavanaugh Sorry for not enough information. Let me describe for the full details.

Folder structure Assume all index.ts export all from same level folder

Project/
β”œβ”€β”€ src/
β”‚   β”œβ”€β”€ index.ts
β”‚   β”œβ”€β”€ model/
β”‚   β”‚    β”œβ”€β”€ index.ts
β”‚   β”‚    └── foo.ts
β”‚   └── function/
β”‚        β”œβ”€β”€ index.ts
β”‚        └── baz/
β”‚             β”œβ”€β”€ index.ts
β”‚             └── bar.ts
β”œβ”€β”€ webpack.config.js
└── tsconfig.json

To import Foo from foo.ts in bar.ts, you have to write the following.

import { Foo } from "../../model";

The problem become more serious when you have more level of nested folder.

In Webpack, we can use resolve to handle this (Support babel):

module.exports = {
  //...
  resolve: {
    alias: {
      "~": path.resolve(__dirname,  "src")
    }
  }
};

In Jest, we can use moduleNameMapper:

"moduleNameMapper": {
  "^~/(.*)": "<rootDir>/src/$1"
}

We also have tsconfig.json to support it in VSCode.

{
  "compilerOptions": {
    "rootDir": ".",
    "paths": {
      "~/*": ["src/*"]
    }
  }
}

Then, we can write the following instead

import { Foo } from "~/model";

These are enough when we are writing an application. However, when I try to write a package in TypeScript, I have to transpile it to JavaScript with TypeScript declaration.

Most of the TypeScript transpilers either does not transpile type declaration or use tsc, for example, babel suggest using tsc --emitDeclarationsOnly for declaration as babel does not do type-checking.

I have tested out how tsc --emitDeclarationsOnly. It emits ~/model without modification. There does not seem to have any tools to handle this for declarations. So, we have to fallback to β€œβ€¦/…/” because we have to support declarations.

This feature request is some sort of configuration that we can support alias feature in emitting declarations. If TypeScript can resolve with paths, it should able to emit declarations with correct paths.

36reactions
shuyums2commented, May 30, 2019

Voting for this to be supported

Read more comments on GitHub >

github_iconTop Results From Across the Web

Typescript declaration file created with alias instead of ...
When I try to consume the library in another typescript app, the build fails due to invalid type declaration file which is being...
Read more >
TSConfig Reference - Docs on every TSConfig option
Offers a way to configure the root directory for where declaration files are emitted. example β”œβ”€β”€ index.ts β”œβ”€β”€ package.json └── tsconfig.json.
Read more >
Creating type aliases
What a type alias is in TypeScript and the cases where they are useful. ... Now update the score variable declarations to use...
Read more >
How to use module path aliases in Visual Studio Code, ...
Typescript does not emit the resolved aliased path to JavaScript, which means that without some help, your JavaScript application won't beΒ ...
Read more >
Typescript – How to solve the problem with unresolved path ...
This error actually points to the transpiled('emitted') JavaScript .js file containing the line with the path alias. The actual cause of theΒ ...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found