Skip to content

Allow overwriting types within projects #28922

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

Closed
5 tasks done
aminpaks opened this issue Dec 8, 2018 · 4 comments
Closed
5 tasks done

Allow overwriting types within projects #28922

aminpaks opened this issue Dec 8, 2018 · 4 comments
Labels
Question An issue which isn't directly actionable in code

Comments

@aminpaks
Copy link
Contributor

aminpaks commented Dec 8, 2018

Search Terms

  • overwrite
  • type definition

Suggestion

I would like to have a way to overwrite an existing type (d.ts) provided by a module within node_module folder.

Use Cases

Recently we put some effort to update jest types within DefinitelyType project but the maintainers are against the changes cause it adds more constraints and their argument is not everyone wants to strongly type their tests.

This opens up for discussion what if some want to not copy the whole definition and just overwrite some? So it would be very useful to overwrite provided types within project.

Examples

The main type definition would be using any to ease the use of library for everyone

// node_modules/@types/jest/index.d.ts
...
interface SpyInstance<T, Y extends any[]> extends MockInstance<any, Y> {}

and we overwrite it within our project as following to benefit all the types:

// Syntax could be different, I won't suggest any
declare namespace jest {
  interface SpyInstance<T, Y extends any[]> extends MockInstance<T, Y> {} 
}

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

This opens up for discussion what if some want to not copy the whole definition and just overwrite some? So it would be very useful to overwrite provided types within project.

You can always just fork the @types package in situations like this. Generally, what you do is take the folder for the types you're intrested in, say, @types/jest, then copy that jest folder into a types folder within your project (not in node_modules, where you found it). Then adjust your dependency version in your package.json from the number to the local path, eg "@types/jest": "file:./types/jest". Then install packages again to let your local copy override/be symlinked over the one in node_modules, and edit as you please. The exact behavior is a little different depending on which npm version you use (older ones copy the folder rather than symlink it - on some versions the file: url fragment may not be required), but the process is generally the same.

This isn't necessarily perfect, but it works today and gets you the flexibility you want.

@weswigham weswigham added the Question An issue which isn't directly actionable in code label Dec 10, 2018
@aminpaks
Copy link
Contributor Author

Of course it will work. My request is actually a design improvement and not a solution to incompatibility problem.
We could keep the maintanance to the minimum not because we didn’t like the way the library was typed but because it was intentionally done like that. The customization details to type more strongly should come from the maintainers of the project.

To me your solution simply burden the end user without discussion

@nasreddineskandrani
Copy link

nasreddineskandrani commented Dec 13, 2018

hi @weswigham

[proposal]
what we can do is to add the ability to inherit a namespace and overwrite inside...
( "overwrite" and not "override" please)

//@types/jest
declare namespace jest {
 interface SpyInstance<T, Y extends any[]> extends MockInstance<any, Y> {}
}

// a 3rd party library
declare namespace customJestForLibrary inherit jest {
 interface SpyInstance<T, any> extends MockInstance<T, Y> {} 
}

// my project
declare namespace customJestForProject inherit jest {
 interface SpyInstance<T, Y extends any[]> extends MockInstance<T, Y> {} 
}

In my project i need to make sure to use customJestForProject namespace for jest types.

I saw also one person posted about namespace overwrite (another proposal)
=> #10187 (comment)

thank you

@typescript-bot
Copy link
Collaborator

This issue has been marked as 'Question' and has seen no recent activity. It has been automatically closed for house-keeping purposes. If you're still waiting on a response, questions are usually better suited to stackoverflow.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Question An issue which isn't directly actionable in code
Projects
None yet
Development

No branches or pull requests

4 participants