Coder Social home page Coder Social logo

ts-react-express-boilerplate's People

Stargazers

 avatar

ts-react-express-boilerplate's Issues

Typescript: keyof and Lookup Types

js

function prop(obj, key) {
  return obj[key]
}

ts

function prop(obj:{}, key: string) {
  return obj[key] 
}

eg:

 const todo = {
      id : 1,
      text : 'sth',
      due : new Date(2019,12,22)
 }
 const id = prop(todo,'id') // any
 const text = prop(todo,'text') //any 
 const due = prop(todo,'due') // any
 Without further information, TypeScript can not know which value will be passed for 
 the key parameter, so it can not infer a more specific return type for the prop
 function. 

use keyof

 function prop<T, K extends keyof T>(obj : T, key: K){
     return obj[key]
 }
 const id = prop(todo,'id') // number
 const text = prop(todo,'text') // string
 const due = prop(todo,'due') // Date

react hooks effect

React Effect
function Counter() {
  const [count, setCount] = React.useState(0);
  React.useEffect(() => {
    const id = setInterval(() => {
      setCount(c => c + 1);
    }, 1000);
    // Tips effect 可选的清除机制。每个effct 都可以返回一个清除函数。
    return () => clearInterval(id);
  }, []); // only re-run the effect if count changes
  return <span>{count}</span>;
}

javascript closures

From

You do not know js

闭包就是函数能够记住并访问它的词法作用域,即使当这个函数在它的词法作用域之外执行时

ps 函数访问外部变量
var MyModules = (function Manager() {
	var modules = {};

	function define(name, deps, impl) {
		for (var i=0; i<deps.length; i++) {
			deps[i] = modules[deps[i]];
		}
		modules[name] = impl.apply( impl, deps );
	}

	function get(name) {
		return modules[name];
	}

	return {
		define: define,
		get: get
	};
})();

MyModules.define( "bar", [], function(){
	function hello(who) {
		return "Let me introduce: " + who;
	}

	return {
		hello: hello
	};
} );


MyModules.define("foo",["bar"],function(bar){
    var hungry = "hippo";
    function awesome() {
        console.log(bar.hello(hungry).toUpperCase())
    }
    return {
        awesome : awesome
    }
})

var foo = MyModules.get( "foo" );
foo.awesome()

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.