Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
67 changes: 67 additions & 0 deletions types/mpv-script/index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -312,8 +312,75 @@ declare namespace mp {
}
}

/**
* A convenient alias to `mp.msg.info`.
*/
declare function print(...msg: unknown[]): void;

/**
* Like `print` but also expands objects and arrays recursively.
*/
declare function dump(...msg: unknown[]): void;

/**
* Make the script exit at the end of the current event loop iteration. This does not terminate mpv itself or other scripts.
* This can be polyfilled to support mpv versions older than 0.40 with:
*/
declare function exit(): void;

// nominal brand for setTimeout returns, in case a random number is passed to clearTimeout
type __TimeoutId = number & { __brand: "setTimeout" };
type __IntervalId = number & { __brand: "setInterval" };

/**
* @param fn callback for each interval
* @param delay delay in millisecond
* @param args args for `fn`
* @returns id
*/
declare function setTimeout<TArgs extends any[]>(
fn: (...args: TArgs) => void,
delay?: number,
...args: TArgs
): __TimeoutId;

/**
* @param codeString javascript code
* @param delay delay in millisecond
* @returns id
*/
declare function setTimeout(codeString: string, delay?: number): __TimeoutId;

/**
* Cancels a scheduled timeout
*/
declare function clearTimeout(id: __TimeoutId): void;

/**
* @param fn callback for each interval
* @param delay delay to first start and interval(millisecond)
* @param args args for `fn`
* @returns id
*/
declare function setInterval<TArgs extends any[]>(
fn: (...args: TArgs) => void,
delay?: number,
...args: TArgs
): __IntervalId;

/**
* @param codeString javascript code
* @param delay delay in millisecond
* @returns id
*/
declare function setInterval(codeString: string, delay?: number): __IntervalId;

/**
* Stop a recurring timer
*/
declare function clearInterval(id: __IntervalId): void;

/**
* note: compilerOptions.module in tsconfig/jsconfig should be set properly otherwise it might not resolve shape of the exports
*/
declare function require(mod: string): any;
34 changes: 34 additions & 0 deletions types/mpv-script/mpv-script-tests.ts
Original file line number Diff line number Diff line change
Expand Up @@ -184,3 +184,37 @@ if (osd_size) {
// $ExpectType number | undefined
osd_size.aspect;
}

// $ExpectType __IntervalId
const interval_id = setInterval(
function(foo, bar) {
// $ExpectType string
const a = foo;
// $ExpectType number
const b = bar;
},
1000,
"foo",
1,
);

// @ts-expect-error
clearInterval(100);

// $ExpectType __TimeoutId
const timeout_id = setTimeout(
function(foo, bar) {
// $ExpectType string
const a = foo;
// $ExpectType number
const b = bar;
},
1000,
"foo",
1,
);

// @ts-expect-error
clearTimeout(interval_id);

clearTimeout(timeout_id);