TypeScript 3.1
一些浏览器厂商特定的类型从lib.d.ts中被移除
TypeScript 内置的.d.ts库(lib.d.ts等)现在会部分地从 DOM 规范的 Web IDL 文件中生成。 因此有一些浏览器厂商特定的类型被移除了。
点击这里查看被移除类型的完整列表:
CanvasRenderingContext2D.mozImageSmoothingEnabledCanvasRenderingContext2D.msFillRuleCanvasRenderingContext2D.oImageSmoothingEnabledCanvasRenderingContext2D.webkitImageSmoothingEnabledDocument.caretRangeFromPointDocument.createExpressionDocument.createNSResolverDocument.execCommandShowHelpDocument.exitFullscreenDocument.exitPointerLockDocument.focusDocument.fullscreenElementDocument.fullscreenEnabledDocument.getSelectionDocument.msCapsLockWarningOffDocument.msCSSOMElementFloatMetricsDocument.msElementsFromRectDocument.msElementsFromPointDocument.onvisibilitychangeDocument.onwebkitfullscreenchangeDocument.onwebkitfullscreenerrorDocument.pointerLockElementDocument.queryCommandIndetermDocument.URLUnencodedDocument.webkitCurrentFullScreenElementDocument.webkitFullscreenElementDocument.webkitFullscreenEnabledDocument.webkitIsFullScreenDocument.xmlEncodingDocument.xmlStandaloneDocument.xmlVersionDocumentType.entitiesDocumentType.internalSubsetDocumentType.notationsDOML2DeprecatedSizePropertyElement.msContentZoomFactorElement.msGetUntransformedBoundsElement.msMatchesSelectorElement.msRegionOverflowElement.msReleasePointerCaptureElement.msSetPointerCaptureElement.msZoomToElement.onwebkitfullscreenchangeElement.onwebkitfullscreenerrorElement.webkitRequestFullScreenElement.webkitRequestFullscreenElementCSSInlineStyleExtendableEventInitExtendableMessageEventInitFetchEventInitGenerateAssertionCallbackHTMLAnchorElement.MethodsHTMLAnchorElement.mimeTypeHTMLAnchorElement.namePropHTMLAnchorElement.protocolLongHTMLAnchorElement.urnHTMLAreasCollectionHTMLHeadElement.profileHTMLImageElement.msGetAsCastingSourceHTMLImageElement.msGetAsCastingSourceHTMLImageElement.msKeySystemHTMLImageElement.msPlayToDisabledHTMLImageElement.msPlayToDisabledHTMLImageElement.msPlayToPreferredSourceUriHTMLImageElement.msPlayToPreferredSourceUriHTMLImageElement.msPlayToPrimaryHTMLImageElement.msPlayToPrimaryHTMLImageElement.msPlayToSourceHTMLImageElement.msPlayToSourceHTMLImageElement.xHTMLImageElement.yHTMLInputElement.webkitdirectoryHTMLLinkElement.importHTMLMetaElement.charsetHTMLMetaElement.urlHTMLSourceElement.msKeySystemHTMLStyleElement.disabledHTMLSummaryElementMediaQueryListListenerMSAccountInfoMSAudioLocalClientEventMSAudioLocalClientEventMSAudioRecvPayloadMSAudioRecvSignalMSAudioSendPayloadMSAudioSendSignalMSConnectivityMSCredentialFilterMSCredentialParametersMSCredentialsMSCredentialSpecMSDCCEventMSDCCEventInitMSDelayMSDescriptionMSDSHEventMSDSHEventInitMSFIDOCredentialParametersMSIceAddrTypeMSIceTypeMSIceWarningFlagsMSInboundPayloadMSIPAddressInfoMSJitterMSLocalClientEventMSLocalClientEventBaseMSNetworkMSNetworkConnectivityInfoMSNetworkInterfaceTypeMSOutboundNetworkMSOutboundPayloadMSPacketLossMSPayloadBaseMSPortRangeMSRelayAddressMSSignatureParametersMSStatsTypeMSStreamReaderMSTransportDiagnosticsStatsMSUtilizationMSVideoPayloadMSVideoRecvPayloadMSVideoResolutionDistributionMSVideoSendPayloadNotificationEventInitPushEventInitPushSubscriptionChangeInitRTCIdentityAssertionResultRTCIdentityProviderRTCIdentityProviderDetailsRTCIdentityValidationResultScreen.deviceXDPIScreen.logicalXDPISVGElement.xmlbaseSVGGraphicsElement.farthestViewportElementSVGGraphicsElement.getTransformToElementSVGGraphicsElement.nearestViewportElementSVGStylableSVGTests.hasExtensionSVGTests.requiredFeaturesSyncEventInitValidateAssertionCallbackWebKitDirectoryEntryWebKitDirectoryReaderWebKitEntriesCallbackWebKitEntryWebKitErrorCallbackWebKitFileCallbackWebKitFileEntryWebKitFileSystemWindow.clearImmediateWindow.msSetImmediateWindow.setImmediate
推荐:
如果你的运行时能够保证这些名称是可用的(比如一个仅针对 IE 的应用),那么可以在本地添加那些声明,例如:
对于Element.msMatchesSelector,在本地的dom.ie.d.ts文件里添加如下代码:
interface Element {
  msMatchesSelector(selectors: string): boolean;
}
相似地,若要添加clearImmediate和setImmediate,你可以在本地的dom.ie.d.ts里添加Window声明:
interface Window {
  clearImmediate(handle: number): void;
  setImmediate(handler: (...args: any[]) => void): number;
  setImmediate(handler: any, ...args: any[]): number;
}
细化的函数现在会使用{},Object和未约束的泛型参数的交叉类型
下面的代码如今会提示x不能被调用:
function foo<T>(x: T | (() => string)) {
  if (typeof x === 'function') {
    x();
    //      ~~~
    // Cannot invoke an expression whose type lacks a call signature. Type '(() => string) | (T & Function)' has no compatible call signatures.
  }
}
这是因为,不同于以前的T会被细化掉,如今T会被扩展成T & Function。 然而,因为这个类型没有声明调用签名,类型系统无法找到通用的调用签名可以适用于T & Function和() => string。
因此,考虑使用一个更确切的类型,而不是{}或Object,并且考虑给T添加额外的约束条件。