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
138 changes: 138 additions & 0 deletions types/alpinejs/alpinejs-tests.ts
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,24 @@ import Alpine, {
Alpine.raw(data);
}

{
// Alpine.transaction
// introduced in 3.16.0

const data = Alpine.reactive({ count: 1 });

// $ExpectType Promise<void>
Alpine.transaction(() => {
data.count = 2;
data.count = 3;
});

// $ExpectType Promise<void>
Alpine.transaction(async () => {
await Promise.resolve();
});
}

{
// Alpine.version
// $ExpectType string
Expand Down Expand Up @@ -86,6 +104,21 @@ import Alpine, {
});
}

{
// Alpine.setErrorHandler
// introduced in 3.16.0

// $ExpectType void
Alpine.setErrorHandler((error, el, expression) => {
// $ExpectType Error
error;
// $ExpectType ElementWithXAttributes<HTMLElement>
el;
// $ExpectType string | undefined
expression;
});
}

{
// Alpine.closestDataStack
const someNode = document.body;
Expand Down Expand Up @@ -163,6 +196,21 @@ import Alpine, {
Alpine.deferMutations();
}

{
// Alpine.deferInit
// introduced in 3.17.0

const el = document.body;
const prerequisite = Promise.resolve();

// $ExpectType void
Alpine.deferInit(el, prerequisite);

// any value is accepted, it is passed through `Promise.resolve`
// $ExpectType void
Alpine.deferInit(el, "not a promise");
}

{
// Alpine.mapAttributes
// inspired by
Expand Down Expand Up @@ -214,6 +262,62 @@ import Alpine, {
Alpine.setEvaluator(justExpressionEvaluator);
}

{
// Alpine.setRawEvaluator
// introduced in 3.16.0

// $ExpectType void
Alpine.setRawEvaluator((el, expression) => Alpine.evaluate(el, expression));
}

{
// Alpine.evaluateRaw
// introduced in 3.16.0

const el = document.body;

// $ExpectType number
Alpine.evaluateRaw<number>(el, "1 + 1");

// $ExpectType unknown
Alpine.evaluateRaw(el, "1 + 1", { scope: { two: 2 } });
}

{
// Alpine.initInterceptors
// exposed on the Alpine object since 3.16.0

const data: Record<string, unknown> = {};

// $ExpectType void
Alpine.initInterceptors(data);

// $ExpectType void
Alpine.initInterceptors(data, (callback) => callback);
}

{
// Alpine.injectMagics
// exposed on the Alpine object since 3.16.0

const el = document.body;
const scope: Record<string, unknown> = {};

// $ExpectType Record<string, unknown> & Magics<Record<string, unknown>>
Alpine.injectMagics(scope, el);

const data = { count: 0 };

// $ExpectType { count: number; } & Magics<{ count: number; }>
Alpine.injectMagics(data, el);

// $ExpectType string
Alpine.injectMagics(data, el).$el.tagName;

// $ExpectType number
Alpine.injectMagics(data, el).count;
}

{
// Alpine.mergeProxies
const el = document.body;
Expand Down Expand Up @@ -292,6 +396,18 @@ import Alpine, {
};
},
);

// the interceptor callback receives a cleanup registrar since 3.16.0
Alpine.interceptor<string>((initialValue, getter, setter, path, key, cleanup) => {
// $ExpectType string
path;
// $ExpectType string
key;
// $ExpectType (callback: () => void) => void
cleanup;
cleanup(() => storage.removeItem(`_x_${path}`));
return initialValue;
});
}

{
Expand Down Expand Up @@ -428,6 +544,28 @@ import Alpine, {
const data = Alpine.evaluate(el, expression, { scope: dataProviderContext });
}

{
// Alpine.watch
// available on the Alpine object since 3.13

const data = Alpine.reactive({ count: 1 });

// $ExpectType () => void
Alpine.watch(
() => data.count,
(
// $ExpectType number
newValue,
// $ExpectType number
oldValue,
) => {},
);

const stopWatching = Alpine.watch(() => data.count, () => {});
// $ExpectType () => void
stopWatching;
}

{
// Alpine.initTree
// inspired by
Expand Down
Loading