microsoft / microsoft/TypeScript

NonNullablePartial type - Partial that doesn't allow explicit null/undefined values

オープン
#34,902 コメント 3 件 リアクション 12 件 担当者 0 名 GitHub で見る

まだ誰も着手していません。

Awaiting More Feedback Suggestion
主要言語
Go
スター
111k
フォーク
14.4k
平均マージ
1日 19時間
マージ済み PR(30日)
117

説明

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.

コントリビューションガイド

コントリビューションガイドを開く

はじめの一歩

  1. issue を最後まで読み、次にプロジェクトのコントリビューションガイドを読みます。
  2. 着手することを issue にコメントします — 二人が同じ作業をするのを防げます。
  3. リポジトリをフォークし、ブランチを切って変更します。
  4. issue 番号を参照したプルリクエストを送ります。

調査の方向性

まず、提案されている NonNullablePartial の定義と、issue に記載されている Partial、mapped-type、null、undefined の動作を確認してください。JavaScript の出力を変更せずに、これを型システムの機能として表現できるかを判断してください。完了の条件は、具体的な設計と検証済みのコンパイラ動作があることであり、utility-type の例だけでは不十分です。

索引モデルが issue の本文から書いたものです。

評価

技術スタック
typescript
領域
compilers
issue の種類
機能追加
難易度
5/5
見積もり時間
1週間以上
活発さ
停滞
明瞭さ
おおむね明確
初心者へのやさしさ
30/100

新しい issue をメールで受け取る

初心者向けの GitHub issue を短くまとめたダイジェスト。