Quantcast
Channel: User Ryan Cavanaugh - Stack Overflow
Browsing latest articles
Browse All 38 View Live

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 Article



Comment 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 Article

Comment 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 Article

Comment 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 Article

Comment 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 Article


Comment by Ryan Cavanaugh on How to Instantiate new HTMLElement?

TypeScript doesn't change the meaning of existing code.

View Article

Comment 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 Article

Comment by Ryan Cavanaugh on Is there destructor in TypeScript?

Bad comment. JavaScript isn't Angular.

View Article


Comment by Ryan Cavanaugh on How to suppress "error TS2533: Object is...

Sounds like a lint rule

View Article


Comment 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 Article

Comment 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 Article

Answer 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 Article

How 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 Article


Answer 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 Article

Answer 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 Article


Answer 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 Article

Answer 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 Article


Answer 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 Article

Answer 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 Article

Answer 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 Article

Answer 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 Article


Answer 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 Article


Answer 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 Article

Answer 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 Article

Answer 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 Article


Answer 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 Article

Why 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 Article

Answer 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 Article

Why 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 Article



What 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 Article

Answer 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 Article

Why 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 Article

What 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 Article


Answer 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 Article

Answer 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 Article

How 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 Article


Answer 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

Browsing latest articles
Browse All 38 View Live




Latest Images