Skip to content

CODIANZ/npm.as_

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

1 Commit
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

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
  });
}

About

No description, website, or topics provided.

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published