2016-10-19 14 views
0

타이프 라이터의 v2.0.2 10.11.6선택적으로 콜백을받는 함수를 선언하고 콜백이없는 경우에만 약속을 반환 할 수 있습니까? 엘 캐피 탄을 실행하는 Mac에서

내가 비동기 작업, 및 수행하는 단일 기능이 있습니다 호출,

  • 을 하나 콜백을 소요하고 아무 것도 반환하지 않습니다를 콜백 나중에,
  • 콜백을받지 않으며 나중에 해결되는 약속을 반환합니다.

내가 (https://github.com/psnider/mongodb-adaptor/blob/master/src/ts/MongoDBAdaptor.ts 참조) 유사한 코드가 작동, 을 가지고 있었어요하지만 지금이, 을 실패하고 난 이유를 알아낼 수 없습니다 것 같다!

// these declarations look correct to me, the caller uses one or the other 
declare abstract class SlimDocumentDatabase<T> { 
    create(obj: T): Promise<T> 
    create(obj: T, done: (error: Error, result?: T) => void): void 
} 

class SlimAdaptor<DocumentType> implements SlimDocumentDatabase<DocumentType> { 
    create(obj: DocumentType, done?: (error: Error, result?: DocumentType) => void) : void | Promise<DocumentType> { 
     if (done) { 
      done(undefined, obj) 
      return 
     } else { 
      return Promise.resolve<DocumentType>(obj) 
     } 
    } 
} 

나는이 어떤로 대체하여) 생성의 구현 (에서 반환 형식 사양을 제거하여 컴파일 할 수 있습니다. 그러나 이것은 아주 틀린 것처럼 보인다! 여기

는 타이프 놀이터에서 코드입니다 : http://www.typescriptlang.org/play/index.html#src=declare%20abstract%20class%20SlimDocumentDatabase%3CT%3E%20%7B%0A%20%20%20%20create(obj%3A%20T)%3A%20Promise%3CT%3E%0A%20%20%20%20create(obj%3A%20T%2C%20done%3A%20(error%3A%20Error%2C%20result%3F%3A%20T)%20%3D%3E%20void)%3A%20void%0A%7D%0A%0A%0Aclass%20SlimAdaptor%3CDocumentType%3E%20implements%20SlimDocumentDatabase%3CDocumentType%3E%20%7B%0A%20%20%20%20%2F%2F%20create(obj%3A%20DocumentType)%3A%20Promise%3CDocumentType%3E%0A%20%20%20%20%2F%2F%20create(obj%3A%20DocumentType%2C%20done%3A%20ObjectCallback%3CDocumentType%3E)%3A%20void%0A%20%20%20%20create(obj%3A%20DocumentType%2C%20done%3F%3A%20(error%3A%20Error%2C%20result%3F%3A%20DocumentType)%20%3D%3E%20void)%20%3A%20void%20%7C%20Promise%3CDocumentType%3E%20%7B%0A%20%20%20%20%20%20%20%20if%20(done)%20%7B%0A%20%20%20%20%20%20%20%20%20%20%20%20done(undefined%2C%20obj)%0A%20%20%20%20%20%20%20%20%20%20%20%20return%0A%20%20%20%20%20%20%20%20%7D%20else%20%7B%0A%20%20%20%20%20%20%20%20%20%20%20%20return%20Promise.resolve%3CDocumentType%3E(obj)%0A%20%20%20%20%20%20%20%20%7D%0A%20%20%20%20%7D%0A%0A%7D%0A

답변