microsoft / microsoft/TypeScript
NonNullablePartial type - Partial that doesn't allow explicit null/undefined values
Personne n'a encore pris cette issue.
- Langage dominant
- Go
- Étoiles
- 111k
- Forks
- 14.4k
- Merge moyen
- 1 j 19 h
- PR mergées (30 j)
- 117
Description
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.
Guide de contribution
Ouvrir le guide de contribution
Par où commencer
- Lisez l'issue en entier, puis le guide de contribution du projet.
- Signalez en commentaire que vous la prenez — cela évite que deux personnes fassent le même travail.
- Forkez le dépôt et travaillez sur une branche.
- Ouvrez une pull request qui référence le numéro de l'issue.
Piste de recherche
Commencez par examiner les définitions proposées de NonNullablePartial ainsi que le comportement de Partial, mapped-type, null et undefined décrit dans l’issue. Déterminez si cela peut être exprimé comme une fonctionnalité du système de types sans modifier la sortie JavaScript ; c’est terminé lorsqu’il existe une conception concrète et un comportement du compilateur validé, et pas seulement un exemple de type utilitaire.
Rédigé par le modèle d'indexation à partir du texte de l'issue.
Évaluation
- Stack technique
- typescript
- Domaine
- compilers
- Type d'issue
- Fonctionnalité
- Difficulté
- 5/5
- Temps estimé
- Plus d'une semaine
- Activité
- À l'abandon
- Clarté
- Plutôt claire
- Accessibilité débutants
- 30/100