@codianz/as_
TypeScript icon, indicating that this package has built-in type declarations

0.0.1 • Public • Published

as_

description

Initialize the object type-safely in the right section.

You can easily and safely initialize the object as a return value.

usage

Suppose there is such a type.

/** sample type */
type Result = {
  status: "ok" | "ng",
  code: number;
};
/** explicit */
function foo() : Result {
  return {
    status: "ok",
    code: 100
  };
}

/** inference */
function bar() {
  const p: Result = {
    status: "ok",
    code: 100
  };
  return p;
}

/** damm code... */
function baz() {
  return {
    status: "cancel", /* <- no error!! */
    code: "what?",    /* <- no error!! */
    bad: true         /* <- no error!! */
  } as Result;
}

/** inference with as_ */
function hoge() {
  return as_<Result>({
    status: "ok",
    code: 100
  });
}

Package Sidebar

Install

npm i @codianz/as_

Weekly Downloads

1

Version

0.0.1

License

MIT

Unpacked Size

2.67 kB

Total Files

7

Last publish

Collaborators

  • terukazu.inoue