Quantcast
Channel: User Ryan Cavanaugh - Stack Overflow
Browsing index pages (45 articles)

Answer by Ryan Cavanaugh for What is causing VS Code to ignore _ prefixed...

This isn't a configurable behavior in TypeScript. You'll need to turn eslint back on

View Article


Answer by Ryan Cavanaugh for What does a TypeScript index signature actually...

You are right to be confused. Index signatures mean a few things, and they mean slightly different things depending where and how you ask.First, index signatures imply that all declared properties in...

View Article


What does a TypeScript index signature actually mean?

I've been writing TypeScript for a while and am confused about what an index signature means.For example, this code is legal:function fn(obj: { [x: string]: number }) { let n: number =...

View Article

Comment by Ryan Cavanaugh on what language is the initial compiler of...

TypeScript 0.8, its initial release, is still on npm

View Article

Why am I getting an error "Object literal may only specify known properties"?

I just upgraded from TypeScript 1.5 to the latest and I'm seeing an error in my code:interface Options { /* ... others ... */ callbackOnLocationHash?: boolean;}function f(opts: Options) { /* ... */ }//...

View Article


Answer by Ryan Cavanaugh for Typescript Compiler API does not support bundling?

TypeScript doesn't provide bundling functionality, whether by API or by tsc

View Article

Answer by Ryan Cavanaugh for NodeJS, TypeScript and typescript-collections

You have an internal vs external modules problem.The TypeScript Collections library is written as an internal module -- a standard JavaScript file that you could just throw in to a script tag in a...

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


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

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

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

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


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

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


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

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

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
Browsing index pages (45 articles)


Latest Images