microsoft / microsoft/TypeScript
NonNullablePartial type - Partial that doesn't allow explicit null/undefined values
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
partial, nonnullable, required, undefined
Suggestion
This might seem as a duplicate of #13195, but this issue is different in that it doesn't require a possibly breaking change to the TS engine.
It instead gets advantage of a special generic Partial type that doesn't allow explicitly setting null/undefined.
// these of course sadly don't work
type NonNullablePartial<T> = {
[P in keyof T]?: NonNullable<T[P]>;
};
type NonNullablePartial<T> = {
[P in keyof T]?: T[P] extends null | undefined | infer R ? R : T[P];
};
My suggestion adds type safety by guarding against what would likely be an error, which isn't yet possible with current implementation of Partial.
Use Cases
The reason for this issue is the fact that explicit null / undefined prevent the application of a "default" parameter with these patterns
/* In this example
* default config = { message: 'message' }
* user config = { message: undefined }
*/
Object.assign({}, { message: 'message' }, { message: undefined }) // { message: undefined }
const obj = { // obj = { message: undefined }
... { message: 'message' },
... { message: undefined }
}
This pattern does not suffer from that and an explicitly set undefined is reassigned with a default param. Though, this pattern is impractical for a large set of config variables.
function f({ message = 'message' } = {}) {
console.log(message)
}
f({ message: undefined }); // message - correct!
f({ message: null }); // null - this makes sense, though not what I am looking for
To put it in other words:
I am using a default config object, which I then need to merge with user config object. I need to prevent the user from possibly mistakenly assigning null/undefined values to parameters which would effectively replace and remove the "default" value.
Examples
export interface ResponseErrorTemplate {
code: string;
status: number;
message: string;
errorConstructor: typeof ApolloError;
}
export interface ResponseErrorTemplateInput {
message: string;
errorConstructor: typeof ApolloError;
}
export type ResponseErrorTemplateGetter = (
config?: Partial<ResponseErrorTemplateInput> // Here, keep Partial, but prevent null/undefined
) => ResponseErrorTemplate;
const defaultConfig: ResponseErrorTemplateInput = {
message: 'Not found',
errorConstructor: ApolloError,
};
export const getNotFoundTemplate: ResponseErrorTemplateGetter = config => ({
...defaultConfig,
...config, // if user defines message: undefined here, they replace the default 'Not found'
status: 404,
code: 'NOT_FOUND',
});
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.
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 esaminando le definizioni proposte di NonNullablePartial e il comportamento di Partial, mapped-type, null e undefined descritto nell’issue. Determina se questo può essere espresso come una funzionalità del sistema dei tipi senza modificare l’output JavaScript; il lavoro è completato quando esistono un design concreto e un comportamento del compilatore validato, non solo un esempio di utility-type.
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