microsoft / microsoft/TypeScript
Force Override Declarations Types
Nessuno ha ancora preso questa issue.
- Lingua principale
- Go
- Stelle
- 111k
- Fork
- 14.3k
- Merge medio
- 2g 4h
- PR unite (30g)
- 132
Descrizione
Search Terms
declaration
module
merging
override
interface
Suggestion
Add a way to force override declarations for modules in the workspace. Typescript allows you to create a global.d.ts to add global declarations. How about an override.d.ts or an *.override.d.ts file to override existing modules declarations in the user workspace.
Use Cases
Lets say you install a node module package and type. You see that the type parameter isn't type safe so you use declaration merging to create a more type safe declaration. However, when you use the module type as a variables type or choose to extending it, it automatically uses the types folder declaration first since the type parameter used fits the node_modules declaration type parameter, however that type would not fit the type parameter the user created. This is because declaration merging selects the most appropriate types in order. If the type was to not match the first declaration type it would move on until reaching the appropriate type, which wouldn't work in this use case.
Examples
// Node Module module declaration file
declare module "react-router" {
interface RouteComponentProps<Params extends { [K in keyof Params]?: string | undefined }> {
params: Params;
}
}
// User Defined Type declaration file
export type NoRequired<T extends {}> = {
[C in keyof T]: T[C] extends Required<T>[C] ? never : T[C];
};
declare module "react-router" {
interface RouteComponentProps<Params extends NoRequired<Params> }> {
params: Params;
}
}
// Using the Interface
import { RouteComponentProps } from "react-router";
// I don't want to extend RouteComponentProps<{ page: string }>, but I can.
export interface CRUDComponentProps
extends RouteComponentProps<{ page: string }> {
serverName: string;
clientName: string;
}
As you can see the type is accepted as the node modules declaration type instead of a user defined declaration type. There no other way of overriding it other than removing it manually from the the node_modules type file.
Checklist
My suggestion meets these guidelines:
- A way of force overriding a type (preferably an override.d.ts or an *.override.d.ts file)
- A way to sort the order in which declaration merging happens
Guida per i contributori
Apri la guida per i contributori
Come iniziare
- Leggi tutta la issue e poi la guida ai contributi del progetto.
- Commenta sulla issue per dire che te ne occupi tu — evita che due persone facciano lo stesso lavoro.
- Fai un fork del repository e lavora su un branch.
- Apri una pull request che faccia riferimento al numero della issue.
Direzione di ricerca
Inizia dal global.d.ts dell’issue, dai pattern proposti override.d.ts o *.override.d.ts e dall’esempio di merging delle dichiarazioni di react-router. Esamina come vengono selezionate e unite le dichiarazioni dei moduli, quindi determina il comportamento richiesto per gli override del workspace e l’ordine delle dichiarazioni. Il lavoro sarà completato quando saranno disponibili un design concordato e una coverage per il comportamento di override proposto.
Scritto dal modello di indicizzazione a partire dal testo della issue.
Valutazione
- Stack tecnologico
- typescript
- Ambito
- compilers
- Tipo di issue
- Funzionalità
- Difficoltà
- 5/5
- Tempo stimato
- Più di una settimana
- Stato di attività
- Ferma
- Chiarezza
- Abbastanza chiara
- Idoneità per principianti
- 30/100