microsoft / microsoft/TypeScript

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

Đang mở
#34,902 3 bình luận 12 reaction 0 người được giao Xem trên GitHub

Chưa có ai nhận issue này.

Awaiting More Feedback Suggestion
Ngôn ngữ chính
Go
Star
111k
Fork
14.4k
Merge trung bình
1 ngày 19 giờ
Pull request đã merge (30 ngày)
117

Mô tả

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.

Hướng dẫn đóng góp

Mở hướng dẫn đóng góp

Bắt đầu từ đâu

  1. Đọc hết issue, rồi đọc hướng dẫn đóng góp của dự án.
  2. Bình luận trên issue rằng bạn sẽ nhận — tránh hai người làm cùng một việc.
  3. Fork repository và làm thay đổi trên một nhánh.
  4. Mở pull request có tham chiếu số hiệu của issue.

Hướng nghiên cứu

Bắt đầu bằng việc xem xét các định nghĩa NonNullablePartial được đề xuất và hành vi của Partial, mapped-type, null và undefined được mô tả trong issue. Xác định xem điều này có thể được biểu đạt dưới dạng một tính năng của hệ thống kiểu mà không thay đổi đầu ra JavaScript hay không; được xem là hoàn tất khi có một thiết kế cụ thể và hành vi của compiler đã được xác thực, không chỉ là một ví dụ về utility-type.

Do mô hình lập chỉ mục viết ra từ nội dung của issue.

Đánh giá

Công nghệ
typescript
Lĩnh vực
compilers
Loại issue
Tính năng
Độ khó
5/5
Thời gian dự kiến
Hơn một tuần
Mức độ hoạt động
Đình trệ
Độ rõ ràng
Khá rõ ràng
Mức phù hợp với người mới
30/100

Nhận issue mới trong hộp thư của bạn

Bản tóm tắt ngắn những issue GitHub phù hợp với người mới.