typeguards Questions

1

Is it possible to make a user defined type guard that will let the compiler know that all of the arguments passed to it are defined? I'd like to do something like this: public static all(...valu...
Gigigigli asked 31/10, 2019 at 3:7

5

Can I create a typeguard which asserts that a particular property exists (or has a specific type) in an object. I.e I have an interface Foo: interface Foo { bar: string; baz: number; buzz?: s...
Tisman asked 3/10, 2019 at 8:23

2

Solved

Does type narrowing with the in operator only work for literals or am I missing something? Help me understand why this is happening please. interface A { a: string; } interface B { b: number; } ...
Splay asked 4/5, 2021 at 10:25

2

Solved

Consider I have a python class that has a attributes (i.e. a dataclass, pydantic, attrs, django model, ...) that consist of a union, i.e. None and and a state. Now I have a complex checking functio...
Herbertherbicide asked 30/11, 2022 at 8:11

2

Since TypeScript 3.0 introduced the unknown top type in mid-2018, use of the any type is discouraged. TypeScript also had had long support for succint type-guards with the typeof operator, but typ...
Bergeron asked 16/2, 2021 at 6:1

11

Solved

I am checking for the existence of an object property with a variable holding the property name in question. var myObj; myObj.prop = "exists"; var myProp = "p"+"r"+"o"+"p"; if(myObj.myProp){ al...
Fecit asked 14/6, 2012 at 19:58

5

Solved

In ActionScript, it is possible to check the type at run-time using the is operator: var mySprite:Sprite = new Sprite(); trace(mySprite is Sprite); // true trace(mySprite is DisplayObject);// tr...
Oribel asked 8/10, 2012 at 20:47

1

Solved

I'm trying to create a custom type guard using an instanceof but strangely it isn't working as expected in the else clause This is an example with the related playground link: Playground Link class...
Siloa asked 2/11, 2022 at 10:29

3

Solved

This code fails to compile: const nodeIsUseless = (node: unknown) => node !== null && typeof node === "object" && "type" in node && typeof node.ty...
Tautomer asked 14/8, 2022 at 21:54

1

Solved

I am migrating to a newer version of typescript but started getting this error in type guards. In the else brach, its showing question is of type never rather than showing question is of type Quest...
Rici asked 27/5, 2022 at 9:39

2

I am new to Python and found the following error with swmmtoolbox package. I would really appreciate your comments. Thanks Traceback (most recent call last): File "C:\Users\Hydraulic Group\ana...
Inlaw asked 14/9, 2021 at 9:7

1

is it possible for a type guard function to return other data? For example something like this: function isNumber(value: any): [value is number, reason: string] { if (typeof value === 'number') { ...
Bechtold asked 26/2, 2022 at 17:9

1

Solved

PEP 647 introduced type guards to perform complex type narrowing operations using functions. If I have a class where properties can have various types, is there a way that I can perform a similar t...
Spandrel asked 8/2, 2022 at 7:15

2

Solved

I'm trying to type guard an unknown type const foo = (obj: unknown) => { if (typeof obj === 'object' && obj) { if ('foo' in obj && typeof obj.foo === 'string') { return obj.fo...
Globin asked 25/10, 2021 at 9:17

3

Solved

I'm looking at the reorderable columns example on stackblitz. In particular I see the html <table mat-table [dataSource]="dataSource" cdkDropList cdkDropListOrientation="horiz...
Agreed asked 20/4, 2021 at 22:4

3

Solved

The code const obj = {}; if ('a' in obj) console.log(42); Is not typescript (no error). I see why that could be. Additionally, in TS 2.8.1 "in" serves as type guard. But nevertheless, is there ...
Halfsole asked 7/4, 2018 at 12:20

2

Solved

Shouldn't this compile correctly? I get an error "Property 'hello' does not exist on type 'object'." in the highlighted line. I can access g.hello outside the fat arrow function without problems. ...
Calva asked 5/12, 2018 at 22:1

1

Solved

Typically, a typeguard's type is defined as such: (value: unknown) => value is Type Where the highlighted part is called a type predicate by the documentation: (value: unknown) => **value...
Breathing asked 30/5, 2020 at 3:13

1

Solved

I have been working on an auth service that uses an rxjs behavior subject to store the last retrieved auth object, and triggers a re-fetch if it has expired (or has not been fetched at all yet). M...

1

Solved

There are the following types: type TypeA = { desc: string; name: string; } type TypeB = { desc: string; name: string; age: number; } type TypeC = { desc: string; name: string; age: numb...
Inhambane asked 17/4, 2020 at 10:1

1

Solved

With Typescript 3.7 the nullish coalescing operator was introduced. It would seem to be the perfect type guard for cases like const fs = (s: string) => s const fn = (n: number) => n let a: ...
Wilfredwilfreda asked 14/4, 2020 at 22:21

1

Solved

I'm trying to write a user-defined type guard that tests whether the value it's given has all the properties in a given array. I'm calling this function hasAll and it's implementation and usage in...
Interlocutory asked 23/1, 2020 at 6:18

1

Solved

Is this a lack of the feature in TypeScript or something well-considered that TS compiler can not infer (and then narrow down) the type of an argument if the condition logic is wrapped into a separ...
Congest asked 26/9, 2019 at 23:18

2

I'm trying to narrow down a complex type using a type guard. I would like the false branch of the type guard to understand the complement to the narrowed down type. interface Model { side: 'left' ...
Haygood asked 7/6, 2019 at 16:31

2

Solved

Let's say I have few interfaces A, B, C implementing common Base. interface Base { x: number; y: number; z: number; } interface A extends Base { a: true; } interface B extends Base { b: true...
Steels asked 11/4, 2019 at 20:44

© 2022 - 2024 — McMap. All rights reserved.