Cho chữ ký sau:
export interface INotificationService {
error(message: string, title?: string, autoHideAfter?: number);
}
Làm thế nào tôi có thể gọi hàm error()
không chỉ định title
tham số, nhưng cài đặt autoHideAfter
để nói 1000
?
Cho chữ ký sau:
export interface INotificationService {
error(message: string, title?: string, autoHideAfter?: number);
}
Làm thế nào tôi có thể gọi hàm error()
không chỉ định title
tham số, nhưng cài đặt autoHideAfter
để nói 1000
?
Câu trả lời:
Theo quy định trong tài liệu , sử dụng undefined
:
export interface INotificationService {
error(message: string, title?: string, autoHideAfter? : number);
}
class X {
error(message: string, title?: string, autoHideAfter?: number) {
console.log(message, title, autoHideAfter);
}
}
new X().error("hi there", undefined, 1000);
undefined
để có được hành vi tương tự như loại bỏ hoàn toàn đối số. Chỉ là "bất cứ điều gì không đúng sự thật" sẽ không hoạt động, bởi vì TypeScript thực sự so sánh với void 0
cách viết an toàn hơn undefined
. )
Thật không may, không có gì giống như vậy trong TypeScript (chi tiết hơn ở đây: https://github.com/Microsoft/TypeScript/issues/467 )
Nhưng để giải quyết vấn đề này, bạn có thể thay đổi thông số của mình thành giao diện:
export interface IErrorParams {
message: string;
title?: string;
autoHideAfter?: number;
}
export interface INotificationService {
error(params: IErrorParams);
}
//then to call it:
error({message: 'msg', autoHideAfter: 42});
bạn có thể sử dụng biến tùy chọn theo ?
hoặc nếu bạn có nhiều biến tùy chọn theo ...
, ví dụ:
function details(name: string, country="CA", address?: string, ...hobbies: string) {
// ...
}
Ở trên:
name
bắt buộccountry
là bắt buộc và có giá trị mặc địnhaddress
Là tùy chọnhobbies
là một mảng các thông số tùy chọnMột cách tiếp cận khác là:
error(message: string, options?: {title?: string, autoHideAfter?: number});
Vì vậy, khi bạn muốn bỏ qua tham số tiêu đề, chỉ cần gửi dữ liệu như thế:
error('the message', { autoHideAfter: 1 })
Tôi muốn thay vì tùy chọn này vì cho phép tôi thêm nhiều tham số mà không phải gửi cho người khác.
title
?
Điều này gần giống như câu trả lời của @Brocco, nhưng với một thay đổi nhỏ: chỉ truyền các tham số tùy chọn trong một đối tượng. (Và cũng làm cho đối tượng params tùy chọn).
Nó cuối cùng giống như ** kwargs của Python, nhưng không chính xác.
export interface IErrorParams {
title?: string;
autoHideAfter?: number;
}
export interface INotificationService {
// make params optional so you don't have to pass in an empty object
// in the case that you don't want any extra params
error(message: string, params?: IErrorParams);
}
// all of these will work as expected
error('A message with some params but not others:', {autoHideAfter: 42});
error('Another message with some params but not others:', {title: 'StackOverflow'});
error('A message with all params:', {title: 'StackOverflow', autoHideAfter: 42});
error('A message with all params, in a different order:', {autoHideAfter: 42, title: 'StackOverflow'});
error('A message with no params at all:');
Bạn có thể chỉ định nhiều chữ ký phương thức trên giao diện sau đó có quá tải nhiều phương thức trên phương thức lớp:
interface INotificationService {
error(message: string, title?: string, autoHideAfter?: number);
error(message: string, autoHideAfter: number);
}
class MyNotificationService implements INotificationService {
error(message: string, title?: string, autoHideAfter?: number);
error(message: string, autoHideAfter?: number);
error(message: string, param1?: (string|number), param2?: number) {
var autoHideAfter: number,
title: string;
// example of mapping the parameters
if (param2 != null) {
autoHideAfter = param2;
title = <string> param1;
}
else if (param1 != null) {
if (typeof param1 === "string") {
title = param1;
}
else {
autoHideAfter = param1;
}
}
// use message, autoHideAfter, and title here
}
}
Bây giờ tất cả những thứ này sẽ hoạt động:
var service: INotificationService = new MyNotificationService();
service.error("My message");
service.error("My message", 1000);
service.error("My message", "My title");
service.error("My message", "My title", 1000);
... và error
phương thức INotificationService
sẽ có các tùy chọn sau:
Bạn có thể tạo một phương thức trợ giúp chấp nhận một tham số đối tượng dựa trên các đối số lỗi
error(message: string, title?: string, autoHideAfter?: number){}
getError(args: { message: string, title?: string, autoHideAfter?: number }) {
return error(args.message, args.title, args.autoHideAfter);
}
Bạn có thể thử đặt tiêu đề thành null.
Điều này làm việc cho tôi.
error('This is the ',null,1000)
Bạn có thể làm điều này mà không cần một giao diện.
class myClass{
public error(message: string, title?: string, autoHideAfter? : number){
//....
}
}
sử dụng ?
toán tử như một tham số tùy chọn.
message
vàautoHideAfter
?
được thêm vào trong định nghĩa hàm , nhưng câu hỏi là về việc thực sự gọi hàm.