microsoft / microsoft/TypeScript

Type instantiation for type level generic function

Đang mở
#62,524 4 bình luận 0 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.3k
Merge trung bình
2 ngày 4 giờ
Pull request đã merge (30 ngày)
132

Mô tả

🔍 Search Terms

type instantiation
type level generic function
generic return type

Related: https://github.com/microsoft/TypeScript/issues/40542, https://github.com/microsoft/TypeScript/issues/61133 and maybe https://github.com/microsoft/TypeScript/issues/52035?

Edit: Just found https://github.com/microsoft/TypeScript/issues/55435 and https://github.com/microsoft/TypeScript/issues/40179

✅ Viability Checklist
⭐ Suggestion

#47607 added support for instantiation expressions, but those do not apply at the type level. I understand it'd be ambiguous in some cases (https://github.com/microsoft/TypeScript/pull/47607#issuecomment-1058192563), but in some cases there really isn't a reasonable workaround.

📃 Motivating Example

I actually have a very similar case to the example provided in https://github.com/microsoft/TypeScript/issues/40542

const provideBox = () => {
    const box = <T>(value: T) => ({value});
    return box;
};

type BoxMaker = ReturnType<typeof provideBox>
// type BoxMaker = <T>(value: T) => { value: T; }

type Box1<T> = ReturnType<BoxMaker<T>>;
// Type 'BoxMaker' is not generic.(2315)

const box = undefined as unknown as BoxMaker;

type Box2<T> = ReturnType<typeof box<T>>;
//type Box2<T> = { value: T; }


Compiler Options
{
  "compilerOptions": {
    "strict": true,
    "noImplicitAny": true,
    "strictNullChecks": true,
    "strictFunctionTypes": true,
    "strictPropertyInitialization": true,
    "strictBindCallApply": true,
    "noImplicitThis": true,
    "noImplicitReturns": true,
    "alwaysStrict": true,
    "esModuleInterop": true,
    "declaration": true,
    "target": "ES2017",
    "module": "ESNext",
    "moduleResolution": "node"
  }
}

Playground Link: Provided

💻 Use Cases
  1. What do you want to use this for?

In our case we have a more complex provideBox that accept arguments. We also need to rely on type inference for the generic type of the box function and its return value;

  1. What shortcomings exist with current approaches?

I cannot extract and export a generic type for a return value of a generic function if I don't have direct reference to the function.

  1. What workarounds are you using in the meantime?

The only workaround I found is to use a dummy runtime value to apply the type, and use and instantiation expression on that dummy value (see Box2 above)

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 cách tái hiện ví dụ tạo động lực trong liên kết Playground được cung cấp, sau đó đọc các issue liên quan và PR #47607 để hiểu hành vi hiện có của các biểu thức khởi tạo và những điểm mơ hồ đã được nêu. Công việc được xem là hoàn tất khi một dạng ở cấp độ kiểu có thể trích xuất kiểu trả về generic mà không cần một giá trị runtime giả, tạo ra kết quả Box2 như mong đợi trong khi vẫn giữ nguyên hành vi hiện có.

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
35/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.