1
1
import Service from '@ember/service' ;
2
- import { tracked } from '@glimmer/tracking' ;
3
2
import { task , timeout } from 'ember-concurrency' ;
4
3
import { A , type NativeArray } from '@ember/array' ;
5
4
import type { ComponentLike } from '@glint/template' ;
@@ -8,29 +7,36 @@ export type ToastData = {
8
7
component ?: ComponentLike < CustomToastSignature > ;
9
8
title ?: string ;
10
9
message ?: string ;
11
- options : ToastOptions | Record < string , unknown > ;
10
+ options : ToastOptions | CustomToastOptions ;
12
11
} ;
13
12
14
- export type ToastOptions = {
13
+ interface BaseOptions {
15
14
icon ?: string | ComponentLike < { Element : Element } > ;
16
15
closable ?: boolean ;
17
16
timeOut ?: number | null ;
17
+ }
18
+
19
+ export interface ToastOptions extends BaseOptions {
18
20
type ?: 'error' | 'success' | 'warning' ;
19
- } ;
21
+ }
22
+
23
+ export interface CustomToastOptions extends BaseOptions {
24
+ [ key : string ] : unknown ; // Allows additional properties
25
+ }
20
26
21
27
export interface CustomToastSignature {
22
28
Args : {
23
- options : Record < string , unknown > ;
29
+ // ToastOptions shouldn't be needed, but I don't know how to do type assertions in templates
30
+ options : ToastOptions | CustomToastOptions ;
24
31
close : ( ) => void ;
25
32
} ;
26
33
}
27
34
28
35
export default class ToasterService extends Service {
29
- // TS was complaining without the explicit NativeArray import and type here, not sure why.
30
36
// TODO: Replace A with a regular array
31
- @ tracked toasts : NativeArray < ToastData > = A < ToastData > ( [ ] ) ;
37
+ toasts : NativeArray < ToastData > = A < ToastData > ( [ ] ) ;
32
38
33
- displayToast = task ( async ( toast : ToastData ) => {
39
+ private displayToast = task ( async ( toast : ToastData ) => {
34
40
if ( typeof toast . options [ 'timeOut' ] === 'undefined' ) {
35
41
toast . options [ 'timeOut' ] = null ;
36
42
}
@@ -49,7 +55,7 @@ export default class ToasterService extends Service {
49
55
} ) ;
50
56
51
57
show (
52
- component : ComponentLike < unknown > ,
58
+ component : ComponentLike < CustomToastSignature > ,
53
59
options : Record < string , unknown > = { } ,
54
60
) {
55
61
const toast = {
0 commit comments