MusicKit TypeScript Types
This package provides TypeScript type definitions for Apple's MusicKit JS library. It enables developers to use TypeScript with the MusicKit JS API, offering compile-time type checking and improved IDE autocompletion, which is crucial as the official MusicKit JS library might lack comprehensive native types. The current stable version is 1.2.4. As a type definition package, its release cadence is typically tied to updates or community needs concerning the MusicKit JS library itself rather than following a fixed schedule. It serves as a community-driven effort to enhance the developer experience for those integrating Apple Music into web applications using TypeScript.
Common errors
-
TS2304: Cannot find name 'MusicKit'.
cause TypeScript compiler cannot locate the global MusicKit type definitions.fixVerify that your `tsconfig.json` file includes `"node_modules/musickit-typescript"` in the `typeRoots` array. For example: `"typeRoots": ["node_modules/musickit-typescript", "node_modules/@types"]`. -
ReferenceError: MusicKit is not defined
cause The MusicKit JavaScript library has not been loaded in the runtime environment (e.g., browser or Node.js). This package only provides types, not the runtime code.fixEnsure that the official Apple MusicKit JS library is loaded via a script tag in your HTML document (or equivalent method for your environment) before your application code attempts to access `MusicKit`. -
TS2339: Property 'someNewMethod' does not exist on type 'MusicKit.Something'.
cause The type definitions are out of sync with a newer version of the MusicKit JS library, or a custom extension to MusicKit JS is being used without corresponding type augmentation.fixCheck for an updated version of `musickit-typescript`. If an update is not available or the method is custom, you may need to create a local declaration file (`.d.ts`) to augment the existing types, for example: `declare namespace MusicKit { interface Something { someNewMethod(): void; } }`.
Warnings
- gotcha Incorrect or missing 'typeRoots' configuration in `tsconfig.json` can prevent TypeScript from finding the global `MusicKit` types, leading to 'Cannot find name' errors.
- gotcha Version mismatches between `musickit-typescript` and the actual MusicKit JS library (loaded at runtime) can lead to type inconsistencies. New features in MusicKit JS might lack types, or deprecated features might still be typed.
- gotcha This package only provides type definitions, not the MusicKit JS library itself. Failure to load the actual MusicKit JS library in your HTML (or equivalent) will result in runtime errors despite successful compilation.
Install
-
npm install musickit-typescript -
yarn add musickit-typescript -
pnpm add musickit-typescript
Imports
- MusicKit (global object)
import { MusicKit } from 'musickit-typescript';const musicInstance = MusicKit.getInstance();
- MusicKit.API (type)
import { API } from 'musickit-typescript';let api: MusicKit.API;
- MusicKit.Player (type)
import { Player } from 'musickit-typescript';function initializePlayer(player: MusicKit.Player) { /* ... */ }
Quickstart
npm install --save-dev musickit-typescript
// Then, update your tsconfig.json:
// Make sure 'node_modules/musickit-typescript' is included in 'typeRoots'
// alongside 'node_modules/@types' to ensure TypeScript finds the definitions.
{
"compilerOptions": {
"typeRoots": [
"node_modules/musickit-typescript",
"node_modules/@types"
],
"target": "es2018",
"module": "esnext",
"lib": ["dom", "esnext"]
},
"include": ["src/**/*.ts"]
}
// Example usage in your TypeScript file (assuming MusicKit JS is loaded in HTML):
// Make sure to define your developer token or get it from your backend
declare const MusicKit: MusicKit.MusicKitInstance;
const developerToken = process.env.APPLE_MUSIC_DEV_TOKEN ?? 'YOUR_DEVELOPER_TOKEN_HERE';
async function initMusicKit() {
try {
await MusicKit.configure({
developerToken: developerToken,
app: {
name: 'My Awesome App',
build: '1.0.0'
}
});
const music = MusicKit.getInstance();
console.log('MusicKit initialized successfully:', music);
if (music.isAuthorized) {
console.log('User is authorized.');
} else {
console.log('User is not authorized. Requesting authorization...');
await music.authorize();
console.log('User authorized!');
}
const player: MusicKit.Player = music.player;
console.log('Current playback state:', player.playbackState);
} catch (error) {
console.error('Failed to initialize MusicKit:', error);
}
}
initMusicKit();