답변:
설명서에 지정된대로 다음을 사용하십시오 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);
운동장 링크 .
불행히도 TypeScript에는 이와 같은 것이 없습니다 (자세한 내용은 https://github.com/Microsoft/TypeScript/issues/467 ).
그러나이 문제를 해결하기 위해 매개 변수를 인터페이스로 변경할 수 있습니다.
export interface IErrorParams {
message: string;
title?: string;
autoHideAfter?: number;
}
export interface INotificationService {
error(params: IErrorParams);
}
//then to call it:
error({message: 'msg', autoHideAfter: 42});
에 의해 선택적 변수를 사용할 수 있습니다. ?또는에 의해 여러 개의 선택적 변수가있는 ...경우 :
function details(name: string, country="CA", address?: string, ...hobbies: string) {
// ...
}
위 :
name 필요하다country 필수이며 기본값이 있습니다address 선택 사항입니다hobbies 선택적 매개 변수의 배열입니다다른 접근 방식은 다음과 같습니다.
error(message: string, options?: {title?: string, autoHideAfter?: number});
따라서 title 매개 변수를 생략하려면 다음과 같이 데이터를 보내십시오.
error('the message', { autoHideAfter: 1 })
다른 옵션을 보내지 않고도 더 많은 매개 변수를 추가 할 수 있기 때문에 오히려이 옵션을 사용하고 싶습니다.
title하시겠습니까?
이것은 @Brocco의 답변과 거의 동일하지만 약간의 왜곡이 있습니다. 개체의 선택적 매개 변수 만 전달하십시오. 또한 params 객체를 선택적으로 만듭니다.
파이썬의 ** 크 워그와 비슷하지만 정확하게는 아닙니다.
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:');
인터페이스에서 여러 메소드 서명을 지정한 다음 클래스 메소드에서 여러 메소드 오버로드를 가질 수 있습니다.
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
}
}
이제이 모든 것이 작동합니다 :
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);
... 및 error방법 INotificationService에는 다음과 같은 옵션이 있습니다.

title을 null로 설정하려고 할 수 있습니다.
이것은 나를 위해 일했습니다.
error('This is the ',null,1000)
인터페이스없이이 작업을 수행 할 수 있습니다.
class myClass{
public error(message: string, title?: string, autoHideAfter? : number){
//....
}
}
?연산자를 선택적 매개 변수로 사용하십시오 .
message와autoHideAfter
?함수 정의에 추가 되었지만 실제로 함수를 호출 하는 것에 대한 질문입니다 .