Parameters<T> là utility type trích xuất tuple chứa kiểu của tất cả tham số từ một function type T, ví dụ Parameters<typeof Math.max> cho ra [number, number]. ConstructorParameters<T> hoạt động tương tự nhưng dành cho constructor của class, trả về tuple các kiểu tham số khi khởi tạo đối tượng.
- Bên trong, cả hai đều sử dụng infer keyword trong conditional type: T extends (...args: infer P) => any ? P : never để suy luận ra kiểu tham số.
- Chúng đặc biệt hữu ích khi cần forward arguments sang hàm khác, tạo wrapper functions, hoặc xây dựng higher-order functions mà vẫn giữ nguyên type safety.
Parameters<T> is a utility type that extracts a tuple of the types of all parameters from a function type T, for example Parameters<typeof Math.max> gives [number, number]. ConstructorParameters<T> works similarly but for class constructors, returning a tuple of the types used when instantiating the object.
- Internally, both use the infer keyword in a conditional type:
T extends (...args: infer P) => any ? P : never. - They are especially useful when forwarding arguments to another function, creating wrapper functions, or building higher-order functions while preserving full type safety.