364 Matching Annotations
  1. Nov 2020
    1. The typescript compiler will not look at the actual typescript files from lib. Instead it will only use the typescript declaration files (*.d.ts) generated when building the lib project. That's why your lib/tsconfig.json file must contain: "declaration": true,
  2. Oct 2020
    1. This library is built in TypeScript, and for TypeScript users it offers an additional benefit: one no longer needs to declare action types. The example above, if we were to write it in TypeScript with useReducer, would require the declaration of an Action type: type Action = | { type: 'reset' } | { type: 'increment' } | { type: 'decrement' }; With useMethods the "actions" are implicitly derived from your methods, so you don't need to maintain this extra type artifact.
  3. Sep 2020
  4. May 2020
    1. Now, a couple of years later, my guidelines for JS are:

      Advices on using JavaScript from a long time programmer:

      1. Use TypeScript instead.
      2. Push as much logic to the server as possible
      3. Use a framework like Vue or React if you need front interactivity
      4. Don't skip unit tests
    1. less easy when you switch from JavaScript to TypeScript. Running TypeScript on Cloud Functions requires a compilation phase to convert the TypeScript into JavaScript so that it can be executed by the emulator. It adds a bit of a hassle

      a bit of a hassle to compile code that can be executed by the emulator

  5. Apr 2020
    1. I need to specify types of input and output. But then I get speedup due to autocompletion, hints, and linting if for any reason I make a mistake.

      In TypeScript, you spend a bit more time in the variable definition, but then autocompletion, hints, and linting will reward you. It also boosts code readability

    1. In the above code, we:state, that the FunctionType extends  (args: any) => anywe say that the FunctionReturnType is a conditional typeinside we declare the ReturnType and assign it with the return type of our function () => infer ReturnType results in assigning the return type to the ReturnType variablewe check if FunctionType extends  (...args: any) => infer ReturnTypeif the above condition is not met, we assign  any to FunctionReturnTypesince the above condition is always met, we assign the ReturnType to the FunctionReturnTypeBy doing all of the above, we can extract the return type of any function.

      解释infer及相关情况。

    1. Within the extends clause of a conditional type, it is now possible to have infer declarations that introduce a type variable to be inferred. Such inferred type variables may be referenced in the true branch of the conditional type. It is possible to have multiple infer locations for the same type variable.

      使用infer

    1. An ambient declaration introduces a variable into a TypeScriptscope, but has zero impact on the emitted JavaScript program.

      环境Declare将变量引入到TypeScript,不会生成JS代码。用它告诉TypeScript编译器其它组件会提供一个变量。

    Tags

    Annotators

    1. The current answers and the official documentation are outdated. And for those new to TypeScript, the terminology used isn't clear without examples. Below is a list of up-to-date differences.

      关于接口和类型别名,更新的内容

    2. Both can be extended, but again, the syntax differs. Additionally, note that an interface and type alias are not mutually exclusive. An interface can extend a type alias, and vice versa

      接口和类型别名并不互斥,还可以相互扩展。

    1. Any JavaScript style guide that is up-to-date for ES6 is going to cover nearly all TypeScript constructs except for type annotations, so I would start with your favorite JS style and decide on what you want the rules for type annotations to be

      可惜这个问题被关了。因此显得有点老。不过他的建议也不错。使用用JS的风格。

  6. Mar 2020
    1. “undefined as a value” and “undefined as a type” are both written as undefined. Depending on where you use it, it is interpreted as a value or as a type. The same is true for null.

      Depending on location, undefined is interpreted as a value or as a type.

    1. TS 采用结构子类型。其含义是:两个类型,即使表示的业务含义大相径庭,只要结构上有从属关系,就是兼容的。(“等同”也是从属关系的一种)

      TS用结构子类型。不同于C#的名义子类型。

    1. Newer versions of TypeScript can now infer types based on your React.PropTypes (PropTypes.InferProps), but the resulting types can be difficult to use or refer to elsewhere in your code.

      新版TS能通过PropTypes推断类型,但是结果类型不大好用。

  7. Feb 2020
  8. Jan 2020
  9. Dec 2019
  10. Nov 2019
  11. Oct 2019
    1. https://stackoverflow.com/questions/58597842/how-to-provide-default-value-for-union-type-parameter-without-For: https://stackoverflow.com/questions/58597842/how-to-provide-default-value-for-union-type-parameter-without-getting-could-be

      Specific href: https://www.typescriptlang.org/play/#code/C4TwDgpgBAaghgGwK4QCrmgXigcgG6Io5QA+uAJhAGZxILDzIQ4BQLokUACgE4D2YAM4AeVFAgAPYBAB25QbEJoMAPiiYWUKACUIAYz49yogDRRBwHgEsZAczWaoAMigBvKH2AALCDwBc5pY2tlAAvmxUSDJ6wFZ8MlBg-EKi4lKy8opM6JAqABQETLwCAagAlAHFKagqLJHRsfGJyYJ5FdwtwvhKOLX1MXEJSQKthShVAYwoOdBkUZRUNhDk6rhjzGVujjwQwEg8Ca6OWgDa61UAugHdTDgmxx7evteePjx3juHhLAD0P1CoCAWQQsBC7NxQBa0ehTCABPAABjMr18YVWwyEeRwULoDB6mz+HgA1qDwe4cTClPCAIxo7AY1o3IgE-6+fg8MxwBSSSAxZak4AQ9bwgBMdOaIyx6xwLOJAqFVKgeAAzOKGW0oIS+CSwYLydRoXimPCACxqloawlswyc7kSXnScgRKIDJo8-TAQQAEQNuNheVcFKNKGRTx4oUqnWxvsptxUmyOWgMMkEfDBADoEHxbFig7C-HdITHgxAyixwu6Yt7i-71dGaH78bLtSxK56fQ3Yyg8nXpWVZdaOVAuWkHcsgA

    2. Based on examples given in https://github.com/Microsoft/TypeScript/issues/29049

      Specific link: http://www.typescriptlang.org/play/#code/LAKAZgrgdgxgLgSwPZQARigRgDwBVUCmAHnAVACYDOqlcATglAOYB8AFEQFyq4CU3+AN6hUqOgTgQ6aAEQALAgBtFSVAHckdReQCEM1AHoDAQ2q4A3KAC+oOAE8ADgVQBleseaoAvKnkIZoDAotDT0jEzcbnQeTN6+cv6BwUiKBAB0KkxsGJhstAzMvLygoJCwiCjoUABMeIQkZFSoABK4ALIAMgCiqQC2ZHDsSABGAFYC-DyowiCi4pLSqFAEai3tHQAiCMaZPQT9UHBsRaimPJYgNiCl0PDIaBgAzHXEpBTUw0gpBB5DYwJxMA7SgEXjTaw3cr3KoAFheDXeqE+31+bBG4x4kyEIkMBnmUjQ6Jx+MWQMUIIhIHsTh4dAgBFwjmcPno9IuQSgIWRilwdIIAj5jJpLL57OSqQySCyGBhbG5vPpRRKIDIEF6qC64NmolQAA0ADSoACahoAWtYgA

      and: http://www.typescriptlang.org/play/#code/LAKAZgrgdgxgLgSwPZQARigRgDwBVUCmAHnAVACYDOqlcATglAOYB8AFEQFyq4CU3+AN6hUqOgTgQ6aAEQALAgBtFSVAHckdReQCEM1AHoDAQ2q4A3KAC+oOAE8ADgVQBleseaoAvKnnH9AD6+csbKSDKgMCi0NO7M3G50Hkzewf6R0UiKBAB0KkxsGJhstEnMvLygoJCwiCjoUABMeIQkZFSoABK4ALIAMgCi2QC2ZHDsSABGAFYC-DyowiCi4pLSqFAEal29fQAiCKFITEMEo1BwbBWopjyWIDYg1dDwyGgYAMwtxKQU1JNILIEDwTGYCVJgUKUAi8RbWZ61N4NAAs3zaf1QAKBILYU1mPHmQhEhgMqykaDxxLJ60himh8JA9icPDoEAIuEczh89DZ9yiUBiWMUuFZBAEoo5zO5or5mWyeWOhSgyLYQpFbIqVRAZAgw1QAzhy1EqAAGgAaVAATQtAC1rEA

    1. These typeof type guards are recognized in two different forms: typeof v === "typename" and typeof v !== "typename", where "typename" must be "number", "string", "boolean", or "symbol". While TypeScript won’t stop you from comparing to other strings, the language won’t recognize those expressions as type guards.
    1. Let's make the example even easier. function convertDate<T extends string | undefined>(isoDate?: string): T { return undefined } 'undefined' is assignable to the constraint of type 'T' Means: What you return in the function (undefined), matches the constraints of your generic type parameter T (extends string | undefined). , but 'T' could be instantiated with a different subtype of constraint 'string | undefined'. Means: TypeScript does not consider that as safe. What if you defined your function like this at compile time: // expects string return type according to generics // but you return undefined in function body const res = convertDate<string>("2019-08-16T16:48:33Z") Then according to your signature, you expect the return type to be string. But at runtime that is not the case! This discrepancy that T can be instantiated with a different subtype (here string) than you return in the function (undefined) is expressed with the TypeScript error.
    1. async function createRequest( url: URL | string, { az, queries, ...parameters }: Params & { az: "text" } ): Promise<string>; async function createRequest<R>( url: URL | string, { az, queries, ...parameters }: Params & { az?: "json" } ): Promise<R>; async function createRequest<R>( url: URL | string, { az, queries, ...parameters }: Params ): Promise<R | string> {
    1. In the body of the function you have no control over the instantiation by the calling context, so we have to treat things of type T as opaque boxes and say you can't assign to it. A common mistake or misunderstanding was to constraint a type parameter and then assign to its constraint, for example: function f<T extends boolean>(x: T) { x = true; } f<false>(false); This is still incorrect because the constraint only restricts the upper bound, it does not tell you how precise T may really be.
    1. type Type = 'a' | 'b'; type AShape = { a: 'a' }; type BShape = { b: 'b' }; type Props<T extends Type> = { type: T, shape: T extends 'a' ? AShape : BShape, }; class Test<T extends ID> extends React.Component<Props<T>> { render() { const { type, shape } = this.props; switch (type) { case 'a': return <>{shape.a}</>; // Ideally would narrow `shape` here, instead of `AShape | BShape` default: return <>{shape.b}</>; } } } <T type="a" shape={{ a: 'a' }} /> // No error in ideal case <T type="a" shape={{ b: 'b' }} /> // error in ideal case
    1. Index types are really handy when you have an object that could have unknown keys. They're also handy when using an object as a dictionary or associative array. They do have some downsides, though. You can't specify what keys can be used, and the syntax is also a bit verbose, in my opinion. TypeScript provides a solution, however; the Record utility.
  12. Sep 2019
    1. Since TypeScript 1.4 static extensions can be added easily. The TypeScript team changed the lib.d.ts file to use interfaces for all static type definitions. The static type definitions are all named like [Type]Constructor: So if you want to add a static function to type Object, then add your definition to ObjectConstructor.
  13. Aug 2019
  14. Jul 2018
  15. Jul 2017
  16. Oct 2016
    1. April of ‘84

      Rigby's Romance evolved from the fifth chapter of the 1898 typescript version of Such is Life. In that text, the date of Tom Collins' arrival in Echuca is 9 January 1884. Although missing from TS, the date of the record is confirmed by internal evidence, such as Tom Collins’ two arrivals at Deniliquin in the closing pages; ‘2.45, p.m., on a cold winter day, nearly five months after the date of the events just recorded – or, to be precise, on the 3rd of June’ (TS 521); or ‘three o’clock on a Saturday afternoon, six or eight weeks after the date of the events just recorded’ (RR 249).

  17. Jun 2016
  18. Mar 2016
  19. Sep 2015
  20. Aug 2015
    1. In summary:

      1. Class decorators are functions that wrap a constructor function
      2. Property decorators are functions that take a prototype and property name as input and can modify the property
      3. Method decorators are functions that take a prototype, method name and method descriptor as input and return an updated descriptor
    2. let’s implement the logClass decorator..

      This can be simplified slightly to:

      function log(constructor: any) {
          let wrappedConstructor = (...args: any[]) => {
              console.log('creating a new ' + constructor.name, 'with args', JSON.stringify(args));
              let instance: any = Object.create(constructor.prototype);
              constructor.apply(instance, args);
              return instance;
          };
          wrappedConstructor.prototype = constructor.prototype;
          return wrappedConstructor as any;
      }
      
  21. Feb 2015