Repository: TypeScript
Issue: allow local module type declarations on separate files (similar to C .h files)
Labels: Suggestion,Awaiting More Feedback

<!-- 🚨 STOP 🚨 𝗦𝗧𝗢𝗣 🚨 𝑺𝑻𝑶𝑷 🚨

Half of all issues filed here are duplicates, answered in the FAQ, or not appropriate for the bug tracker.

Please help us by doing the following steps before logging an issue:
  * Search: https://github.com/Microsoft/TypeScript/search?type=Issues
  * Read the FAQ, especially the "Common Feature Requests" section: https://github.com/Microsoft/TypeScript/wiki/FAQ

-->

## Search Terms

.d.ts definitions ambient module typing local

<!-- List of keywords you searched for before creating this issue. Write them down here so that others can find this suggestion more easily -->

## Suggestion

I would like to being able to compose typescript modules/components inside my application .i.e. `src/components/Foo.ts` and being able to declare its typings under `src/components/Foo.d.ts` where typescript would either infer those types automatically.

**Disclaimer**: It may happen that this is already possible, but I already researched everywhere and read the documentation as well as trying to declare ambient modules *(which won't work with relative paths)*, so I am trying this as a last resort in case it's indeed not currently possible.

<!-- A summary of what you'd like to see added or changed -->

## Use Cases

Sometimes, when creating components inside an application, some may have a quite extensive type set which usually makes the file difficult to read and take the focus away from the implementation.
Like so:

```
src/component/Foo.ts
##########################
#         type           #
#         type           #
#         type           #
#         type           #
#         type           #
#                        #
#    implementation      #
#    implementation      #
##########################
```



<!--
What do you want to use this for?
What shortcomings exist with current approaches?
-->

This would make it easier for the developer to organise their code similar to C's .h files separating the definition from implementation.


## Examples

<!-- Show how this would be used and what the behavior would be -->

Project structure:
```
src/components
src/components/Foo.ts
src/components/Foo.d.ts
src/components/complex/index.ts
src/components/complets/index.d.ts
```

types are automatically inferred within the main module

### src/components/Foo.ts
```ts
const bar:Bar = {foo:1,bar:2};
export default ...
```

### src/components/Foo.d.ts
```ts
type Bar = {foo:number; bar:number; }
export type ExportedType = string[];
```

types can still be imported from a different component

### src/components/Bar.ts
```ts
import type {ExportedType} from './Foo';
import type {Bar} from './Foo'; // < Error as Bar is not exported
```

This would result in:
```
src/component/Foo.d.ts                    src/components/Foo.ts
##########################                ##########################
#         type           #                #    implementation      #
#         type           #                #    implementation      #
#         type           #                #                        #
#         type           #                #                        #                        
#         type           #                #                        #
##########################                ##########################
```

## Checklist

My suggestion meets these guidelines:

* [ ] This wouldn't be a breaking change in existing TypeScript/JavaScript code
* [x] This wouldn't change the runtime behavior of existing JavaScript code
* [x] 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.)
* [x] This feature would agree with the rest of [TypeScript's Design Goals](https://github.com/Microsoft/TypeScript/wiki/TypeScript-Design-Goals).