Comment by Ryan Cavanaugh on How do I make TypeScript treat my file as a module?
YMMV but in my experience, if you're writing actual code, it's extremely difficult to get more than 20 lines into a file without writing an import. Just logistically, you can't really have more than a...
View ArticleComment by Ryan Cavanaugh on How to define type for a function callback (as...
@SgtPooki post your own answer. That's how the site works.
View ArticleComment by Ryan Cavanaugh on Why am I getting an error "Object literal may...
A "typo" can also manifest if, for example, you change an API somewhere and intentionally break downstream code.
View ArticleComment by Ryan Cavanaugh on Typescript, JSON.parse error: "Type 'null' is...
No, because the very first step of JSON.parse is an implicit coercion of its argument to a string. JSON.parse(32)"succeeds" for the same reason but is not allowed either.
View ArticleComment by Ryan Cavanaugh on How to make Flow and Typescript work together in...
Why not just add the commas for now? TS will accept them.
View ArticleComment by Ryan Cavanaugh on How to Instantiate new HTMLElement?
TypeScript doesn't change the meaning of existing code.
View ArticleComment by Ryan Cavanaugh on Why doesn't Object.keys return a keyof type in...
satisfies doesn't do anything to prevent aliasing with a supertype alias, which is the problem. You would need exact types to solve this "problem"
View ArticleComment by Ryan Cavanaugh on Is there destructor in TypeScript?
Bad comment. JavaScript isn't Angular.
View ArticleComment by Ryan Cavanaugh on How to suppress "error TS2533: Object is...
Sounds like a lint rule
View ArticleComment by Ryan Cavanaugh on Naming of TypeScript union and intersection types
This is the terminology CS type theorists have been since the 70's
View ArticleComment by Ryan Cavanaugh on Typescript: why function without parameters can...
There isn't, and there also isn't a good reason to enforce this. A program with an additional unused parameter isn't plausibly more correct than one without it.
View ArticleAnswer by Ryan Cavanaugh for How to differentiate between a .ts file and a .d.ts
This isn't possible. TypeScript's module resolution algorithm will always prefer the .ts file over a .d.ts file of the same name, since it assumes the latter is a build output of the former. You should...
View ArticleHow do I use namespaces with TypeScript external modules?
I have some code:baseTypes.tsexport namespace Living.Things { export class Animal { move() { /* ... */ } } export class Plant { photosynthesize() { /* ... */ } }}dog.tsimport b =...
View ArticleAnswer by Ryan Cavanaugh for How to relate a version of @types package to the...
The major and minor versions of the DefinitelyTyped packages are supposed to correspond to the major and minor versions of the package they are types for. The patch version increments whenever the...
View ArticleAnswer by Ryan Cavanaugh for Why can't TypeScript modules implement or adhere...
You can force a compile error if a module doesn't adhere to an interface like this (technically non-zero runtime overhead, but unlikely to actually matter):interface foo { bar(): number;}module mod {...
View ArticleAnswer by Ryan Cavanaugh for TypeScript Coding Style Guide?
The TypeScript team doesn't issue an "official" style guide for other projects using TypeScript. The guidelines for working on the compiler itself are both too specific and not broad enough for general...
View ArticleAnswer by Ryan Cavanaugh for Strange behaviour of callback function in...
TypeScript doesn't know that filter's callback runs immediately, so if for example filter's callback ran later (let's say on a setTimeout), your code would crash under a call like this:const a: Args =...
View ArticleAnswer by Ryan Cavanaugh for Can I define a Typescript interface with...
You need to use as const to make a nonwidening string literal type at the key declarations:const KEY_SAUCE = `${NAMESPACE}/sauce` as const;const KEY_CRUST = `${NAMESPACE}/crust` as const;
View ArticleAnswer by Ryan Cavanaugh for In typescript, why is `[boolean, string?]`...
These types are different:// OKconst p1: [boolean, string?] = [true, undefined];// Errorconst p2: [boolean, string] | [boolean] = [true, undefined];
View ArticleAnswer by Ryan Cavanaugh for Is it possible to overload an entire interface...
You can't do this with generic interface declarations, but you can use conditional types to accomplish the same behaviortype Foo<T> = T extends string ? { foo(): void; bar(): void; } : T extends...
View ArticleAnswer by Ryan Cavanaugh for Typescript keyof T type assertion vs type...
Not a bug. See https://www.typescriptlang.org/docs/handbook/2/everyday-types.html#type-assertionsexample3's initializer type is a supertype of IGeneric<IExample>, but example2's is not.
View ArticleAnswer by Ryan Cavanaugh for Accessing Nested Variant in Type Definition
Your Handlers type is incorrect, because it would allow the callhandlers.DATA?.({ y: "Hello "});without error, which is why you're getting an error in DATA:The correct definition, given what you want,...
View ArticleAnswer by Ryan Cavanaugh for Why does TypeScript have both `void` and...
void has special meaning in function return types, and is not an alias for undefined. Thinking of it this way is very wrong. Why?The intent of void is that a function's return value will not be...
View ArticleAnswer by Ryan Cavanaugh for What is the naming convention for interfaces and...
There's no official naming convention though it's common in the broader TS community to not use I for interfaces unless they're explicitly for being implemented by classes like:import type { IUser }...
View ArticleAnswer by Ryan Cavanaugh for TypeScript: get syntax tree
The TypeScript parser doesn't directly produce a tree like that, but you can still use its object model to do all sorts of things. We use it in some tools to do syntax transforms for testing purposes,...
View ArticleAnswer by Ryan Cavanaugh for Can I narrow this down in TypeScript?
You can use a generic type guard function:public init(input?: string): void { function isSpecified<T>(input: null | undefined | T): input is T { return (typeof input !== "undefined") &&...
View ArticleWhy does TypeScript have both `void` and `undefined`?
In TypeScript, you can annotate a function as returning void:function fn1(): void { // OK}function fn2(): void { // Error return 3;}You can also annotate a function to return undefined:function fn3():...
View ArticleAnswer by Ryan Cavanaugh for Do Typescript generics use type erasure to...
The type system is wholly erased. You can see this in the generated code for any generic function.That said, for classes, there's still some runtime information left. You could write this:class A {...
View ArticleWhy doesn't Object.keys return a keyof type in TypeScript?
Title says it all - why doesn't Object.keys(x) in TypeScript return the type Array<keyof typeof x>? That's what Object.keys does, so it seems like an obvious oversight on the part of the...
View ArticleWhat does "all legal JavaScript is legal TypeScript" mean?
I often hear people making statements like thisAll JavaScript code is legal TypeScript codeorTypeScript is a superset of JavaScriptBut yet when I wrote some perfectly legal and reasonable JS code which...
View ArticleAnswer by Ryan Cavanaugh for What does "all legal JavaScript is legal...
All JavaScript code is legal TypeScript codeHere's what this means:TypeScript does not change the syntax of existing JavaScriptTypeScript does not change the behavior of existing JavaScriptTypeScript...
View ArticleWhy does Flow allow mismatched type comparisons when one operand is an array...
The two comparisons below seem equivalent, but only one gets a type error (playground):function test(arr: string[]) { const el = arr[0]; if (el === 2) { } // Error here if (arr[0] === 2) { } // No...
View ArticleWhat does the error "JSX element type '...' does not have any construct or...
I wrote some code:function renderGreeting(Elem: React.Component<any, any>) { return <span>Hello, <Elem />!</span>;}I'm getting an error:JSX element type Elem does not have any...
View ArticleAnswer by Ryan Cavanaugh for In Typescript, what is the difference between...
Interfaces can be extendedinterface A { x: number;}interface B extends A { y: string;}and also augmented (the so-called declaration merging)interface C { m: boolean;}// ... later ...interface C { n:...
View ArticleAnswer by Ryan Cavanaugh for What to use for data-only objects in TypeScript:...
Use Interface, class is not even close.People start writing TypeScript and they suddenly think they have to use classes for some reason. But they don't. Classes are an ES6 feature and they work fine,...
View ArticleHow do I check that a switch block is exhaustive in TypeScript?
I have some code:enum Color { Red, Green, Blue}function getColorName(c: Color): string { switch(c) { case Color.Red: return 'red'; case Color.Green: return 'green'; // Forgot about Blue } throw new...
View ArticleAnswer by Ryan Cavanaugh for const enum in Typescript
Just remove the const modifier.const in an enum means the enum is fully erased during compilation. Const enum members are inlined at use sites. You can't index it by an arbitrary value.In other words,...
View Article
More Pages to Explore .....