genki / ref

ref object for TypeScript

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

ref

The ref object for TypeScript

Install

npm insgall @s21g/ref

Usage

As a variable to refer the property of the state object with checking type and existance.

import ref { Ref, getRef, setRef } from "@s21g/ref"
const state = {name:"Taro", age:20};
const nameRef:Ref<string> = ref({name:state});
const name = getRef(nameRef);
setRef(nameRef, "Jiro");

As an argument to refer the property of the state object with checking type and existance.

const foo = <V,K extends string,S>(
  ref:Ref<V,K,S>,
  value:V,
) => {
  setRef(ref, value);  
};
const state:{count:number, name:string} = {count: 0, name: "foo"};
foo({count:state}, 2);
getRef({count:state})) // => 2

Playground: https://stackblitz.com/edit/typescript-demo-ugx7jw?devToolsHeight=33&file=index.ts

About

ref object for TypeScript


Languages

Language:TypeScript 100.0%