@fenge/types
v0.2.0
Published
A type patch for TypeScript, enhancing type-safe for built-in JavaScript apis.
Downloads
422
Maintainers
Readme
@fenge/types
A type patch for TypeScript, enhancing type-safe for built-in JavaScript apis.
Feature
Without this library:
- 🚨
Array.isArray
returnsany[]
. - 🚨
JSON.parse
returnsany
. - 🚨
new Map()
generatesMap<any, any>
. - 🚨
new Promise()
can reject a nonError
variable. - 🚨
<promise object>.catch
accepts(reason: any) => void | PromiseLike<void>
. - 🚨
<promise object>.then
accepts(reason: any) => void | PromiseLike<void>
for the second parameter.
With this library:
- 👍
Array.isArray
returnsunknown[]
. - 👍
JSON.parse
returnsunknown
. - 👍
new Map()
generatesMap<unknown, unknown>
. - 👍
new Promise()
must reject anError
variable. - 👍
<promise object>.catch
accepts(reason: unknown) => void | PromiseLike<void>
. - 👍
<promise object>.then
accepts(reason: unknown) => void | PromiseLike<void>
for the second parameter.
Usage
Add a triple-slash-directive /// <reference types="@fenge/types" />
at the top of the ts file that serves as the entry point for your application or package. This will make the entire project more type-safe.
Application/Package Entry Point (eg: src/main.ts
or src/app.ts
)
/// <reference types="@fenge/types" />
import foo from "./foo";
Other File (eg: src/other-file.ts
)
console.log(JSON.parse('{"foo":"foo"}').bar.length);
// ^^^^^^^^^^^^^^^^^^^^^^^^^^^ ❌ Object is of type 'unknown'.
Differences Between @total-typescript/ts-reset
- This library only focus on built-in ECMAScript apis. So it doesn't enhance the browser or Node apis like
fetch
. - This library only focus on type-safe, not convenience. So it doesn't contain the apis like
array-includes.d.ts
, which@total-typescript/ts-reset
does.
License
MIT