Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Clean up syntax for importing and using nested type declarations #39935

Open
5 tasks done
quinnlangille opened this issue Aug 6, 2020 · 2 comments
Open
5 tasks done
Labels
Awaiting More Feedback This means we'd like to hear from more people who would be helped by this feature Suggestion An idea for TypeScript

Comments

@quinnlangille
Copy link

Search Terms

  • import
  • import equals
  • nested import

Suggestion

The current syntax for importing nested modules is confusing. For example, to specifically use a single property from an interface I would need to do something like this:

// interface.ts
interface Example {
    Foo: string
}

// file.ts
import { Example } from 'interface.ts';
import Foo = Example.Foo // this is weird, as I'm not actually importing any resource here

From what I understand, import is not importing anything here. It's confusing and looks like it's doing something totally different at first glance. In this case, I could also use type Bind = interfaces.Bind, but I don't want to create a new resource.

A much nicer syntax would be adding a use or take keyword, or adding support for importing nested modules

Use Cases

This suggestion is just for readability and developer experience.

Examples

Importing nested modules with use or take

// interface.ts
interface Example {
    Foo: string
}

// file.ts
import { Example } from 'interface.ts';
take { Foo } from Example;

importing nested interface/namespace values:

// interface.ts
interface Example {
    Foo: string
}

// file.ts
import { Example: { Foo } } from 'interface.ts';

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.
@cruhl
Copy link

cruhl commented Aug 17, 2020

#39865

^ Namespaces could be useful here if this issue was addressed...

@RyanCavanaugh RyanCavanaugh added Awaiting More Feedback This means we'd like to hear from more people who would be helped by this feature Suggestion An idea for TypeScript labels Sep 2, 2020
@dbeckwith
Copy link

Would love to see your second example be possible, it lines up nicely with destructuring syntax

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Awaiting More Feedback This means we'd like to hear from more people who would be helped by this feature Suggestion An idea for TypeScript
Projects
None yet
Development

No branches or pull requests

4 participants