-
Notifications
You must be signed in to change notification settings - Fork 513
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: complete types for modules and core
Closes: #366
- Loading branch information
Showing
20 changed files
with
159 additions
and
68 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,35 +1,91 @@ | ||
import {notice, Stack, defaultStack} from '@pnotify/core'; | ||
import {info, Stack, defaultStack, defaultModules, Notice, ModuleEntry} from '@pnotify/core'; | ||
import * as PNotifyBootstrap4 from '@pnotify/bootstrap4'; | ||
import * as PNotifyAnimate from '@pnotify/animate'; | ||
import * as PNotifyConfirm from '@pnotify/confirm'; | ||
|
||
const myNotice = notice({ | ||
defaultModules.set(PNotifyBootstrap4, {}); | ||
|
||
// This *will not* produce an error, because TypeScript doesn't know the options | ||
// are for PNotifyAnimate. | ||
const entry: ModuleEntry = [PNotifyAnimate, { | ||
inClass: 'someClass', | ||
// This is bad, because if you get the options wrong or they change in a | ||
// future version of PNotify, TypeScript won't tell you about it. | ||
something: true, | ||
}]; | ||
|
||
const entry2: ModuleEntry<typeof PNotifyAnimate> = [PNotifyAnimate, { | ||
inClass: 'someClass', | ||
// This *will* produce an error, as it should. | ||
// something: true, | ||
}]; | ||
|
||
const stack = new Stack({ | ||
dir1: 'up' | ||
}); | ||
|
||
const myNotice = info({ | ||
text: 'Hello.', | ||
hide: false, | ||
closer: false, | ||
sticker: false, | ||
// This is good, because it's a Stack instance. | ||
stack, | ||
// This is bad, because it's just an object. This will produce an error. | ||
// stack: { dir1: 'up' }, | ||
// This will produce an error, because it is the wrong type. | ||
// delay: 'string', | ||
modules: new Map([ | ||
// This should not produce a TypeScript error: | ||
...defaultModules, | ||
[PNotifyAnimate, { | ||
inClass: 'someClass', | ||
}] as PNotifyAnimate.Entry, | ||
inClass: 'bounceIn', | ||
outClass: 'bounceOut', | ||
// This *will* produce a TypeScript error, as it should. | ||
// something: true, | ||
}] as ModuleEntry<typeof PNotifyAnimate>, | ||
[PNotifyConfirm, { | ||
confirm: true, | ||
}] as PNotifyConfirm.Entry, | ||
// This should produce a TypeScript error: | ||
// [PNotifyAnimate, { | ||
// inClass: 'someClass', | ||
// something: 'hello', | ||
// }] as PNotifyAnimate.Entry, | ||
buttons: [ | ||
{ | ||
text: 'Hi', | ||
primary: true, | ||
click: (notice: Notice) => notice.close() | ||
} | ||
] | ||
}] as ModuleEntry<typeof PNotifyConfirm>, | ||
]) | ||
}); | ||
|
||
const stack = new Stack({ | ||
dir1: 'up' | ||
}); | ||
|
||
// Hover over these in your IDE to see what TypeScript says about their value. | ||
stack.dir1; | ||
defaultStack.dir1; | ||
|
||
// Unfortunately, TypeScript doesn't yet provide a way to spread the options | ||
// over the notice, so here, TypeSript says `text` could be | ||
// `string | false | HTMLElement`. | ||
myNotice.text; | ||
// But if you assign the value after the notice is created (which you probably | ||
// shouldn't do usually), | ||
myNotice.text = document.createElement('span'); | ||
// TypeScript knows now that it's an HTMLElement. | ||
myNotice.text; | ||
// And if you assign it back, | ||
myNotice.text = 'Hello.'; | ||
// TypeScript knows it's a string. | ||
myNotice.text; | ||
|
||
myNotice.closer; | ||
myNotice.type; | ||
// It does know that `type` defaults to `"info"` here because we used the `info` | ||
// function. | ||
myNotice.type; | ||
|
||
|
||
// Something you can do is to assert the type of the options on the notice. | ||
const myOptions2 = { | ||
text: 'Hello.' | ||
}; | ||
const myNotice2 = info(myOptions2) as Notice & typeof myOptions2; | ||
|
||
// Now TypeScript knows that `text` is a string. | ||
myNotice2.text; | ||
// But you can't then assign anything else. :( | ||
// myNotice2.text = document.createElement('span'); // TypeScript error. |
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.