unity打字没有候选字

I’m pretty new to TypeScript. I’ve been coding with JavaScript for about 10 years, and I’ve been starting to see problems that I believe can be addressed with TypeScript. I’m still not 100% sure it’s useful in all projects, but the rising tide of library authors migrating is not a coincidence.

我是TypeScript的新手。 我已经使用JavaScript编码了大约10年,并且我开始看到我认为可以使用TypeScript解决的问题。 我仍然不是100%确信它在所有项目中都有用,但是库作者迁移的浪潮并不是巧合。

Unfortunately the idea that if you know JavaScript, you basically already know TypeScript turns out not to be true. Don’t get me wrong, if you’re writing JavaScript then you can definitely learn TypeScript. But it is a learning process. Alas, nothing comes for free.

不幸的是,如果您了解JavaScript,那么您基本上已经知道TypeScript的想法就变成了事实。 别误会,如果您正在编写JavaScript,那么您绝对可以学习 TypeScript。 但这是一个学习过程。 las,没有什么是免费的。

Generics demonstrate why TypeScript can seem daunting.

泛型说明了为什么TypeScript看起来令人生畏。

function getMaxNumChildren<T>(data: T[], childKey: keyof T) {
  return data.reduce((max: number, parent: T) => {
    return max > parent[childKey].length ? max : parent[childKey].length;
  }, 0);
}

This doesn’t look like JavaScript right? It’s completely alien to a JS developer. It looks like a whole other language (namely C#).

这看起来像JavaScript吧? 这完全与JS开发人员无关。 它看起来像一种其他语言(即C#)。

This was a complete blocker for me. I don’t want to switch to being a TypeScript developer, I want to learn it as an extension to JS, and this feels like committing to something completely different.

这对我来说是一个完全的阻碍。 我不想切换为TypeScript开发人员,我想将其作为JS的扩展来学习,这就像在致力于完全不同的事情。

(The moment of realisation)

As with many things in life, all my research was blown out of the water by a simple one-sentence explanation, slightly paraphrased from the TypeScript 5-minute handbook.

就像生活中的许多事情一样,我的所有研究都通过一个简单的单句解释就被淹没了,该解释从《 TypeScript 5分钟手册》中略作解释。

Generics provide arguments to types.

泛型为类型提供参数。

That’s it. That’s all it is.

而已。 仅此而已。

In the space of 5 words TypeScript suddenly became more understandable. There’s still a lot to learn, but this one thing changed it from chaos to a readable extension to JS for me.

在5个字的间隔内,TypeScript突然变得更容易理解。 还有很多东西要学习,但是这一件事将它从混乱变成了对JS的可读扩展。

function getMaxNumChildren<T>(data: T[], childKey: keyof T) {
  return data.reduce((max: number, parent: T) => {
    return max > parent[childKey].length ? max : parent[childKey].length;
  }, 0);
}


interface MyType {
  hello: string;
}


getMaxNumChildren<MyType>([{ hello: 'world' }], 'hello');


/**
 * Compare that function declaration to the function call:
 * (funky spacing to help highlight the similarities)
 * function getMaxNumChildren<T>     (data: T[]           , childKey: keyof T)
 *          getMaxNumChildren<MyType>([{ hello: 'world' }], 'hello')
 */


/**
 * TypeScript can infer the type from the argument you pass in,
 * so the generic should be ommitted.
 * The following three function calls are essentially the same:
 * (funky spacing to help highlight the similarities)
 */
getMaxNumChildren                   ([{ hello: 'world' }], 'hello');
getMaxNumChildren<MyType>           ([{ hello: 'world' }], 'hello');
getMaxNumChildren<{ hello: string }>([{ hello: 'world' }], 'hello');

Generics can be useful if, for example, you want to write a function that can perform an action on more than one Type.

例如,如果您想编写一个可以对多个Type执行动作的函数,则泛型可能很有用。

A function that calculates something based on the .length of the argument passed in, and you don’t mind if it’s a string or an array (because both have a .length property).

一个函数,该函数根据传入的参数的.length来计算内容,并且您不介意它是字符串还是数组(因为它们都具有.length属性)。

Or you might not know what Type is going to be passed in to a function, but you want the Type checker to check that the return Type is the same as the Type that was passed in.

或者,您可能不知道将要传递给函数的Type,但是您希望Type检查器检查返回的Type是否与传递的Type相同。

(Why I got stuck)

I’ve always liked learning about code by inspecting, and debugging it. Everyone has a different approach, but that’s the one that has worked best for me. This experience taught me a valuable lesson though. When it comes to understanding core features of the language it’s easier to find an explanation first.

我一直喜欢通过检查和调试代码来学习代码。 每个人都有不同的方法,但这是最适合我的方法。 但是,这种经验教会了我一个宝贵的教训。 在理解语言的核心功能时,首先找到一个解释比较容易。

All the best,Nick

祝一切顺利,尼克



翻译自: https://medium.com/@nickhealweb/typescript-generics-explained-in-5-words-cf223a20901b

unity打字没有候选字