Skip to main content

Questions tagged [typeguards]

Use for questions about narrowing types with predefined (like "instanceof", "typeof" and "in" operators) or user-defined guards.

typeguards
2 votes
1 answer
40 views

TypeScript type guards. How to get type narrowing AND suggestions?

I have assertIsDefined type guard with expected value. I need to type narrowing to expected value with type suggestions. For type narrowing i need to add | unknown to generic type, and suggestions ...
Egor's user avatar
  • 23
2 votes
1 answer
48 views

Why is the behavior inconsistent when intersecting type guards?

I want to use type guards so that hasNext can filter out types. type Queue1 = { operation: 'move'; offset: number; }; type Queue2 = { operation: 'eat'; food: string; }; type Queue3 = { ...
Foh's user avatar
  • 23
1 vote
1 answer
22 views

Trying to add a typeguard dependent on content existing in the element in Typescript

I'm trying to create a conditional in Typescript that checks if a specific word already exists in the name. I have this function: isOrganic() { for (let i = 0; i < this.items.length; i++) { ...
MyDisplayName's user avatar
3 votes
1 answer
93 views

Why write the return type of a type guard as `this is (…) & this` in TypeScript?

I'm reading the Classes section of the handbook, but there is a difference that makes me confused. In the following examples: In Example 1, they first explicitly wrote the return type this is ...
NeoZoom.lua's user avatar
  • 2,609
1 vote
1 answer
56 views

Type guarding class in switch statement

I haven't use Typescript in many years and can't remember nor find how to type guard many classes inside a switch statement correctly. class A {} class B {} class C {} type OneOfThem = A | B | C; ...
Ettapp's user avatar
  • 878
1 vote
1 answer
64 views

How to define TypeScript return type in a method to infer variable existence based on a parameterized type?

I have a TypeScript class called Design which contains a method named checkFetched. This method is designed to check if a property of type DesignData exists within the class instance based on a ...
Nathan's user avatar
  • 23
1 vote
0 answers
31 views

in typescript, how to write a generic function that returns the typeof [duplicate]

Here is my TypeScript code: interface TheType{ name:string } function get_ts_type<T>(): string { return // todo: implement this } function test_it(){ const ans=get_ts_type<TheType>() ...
yigal's user avatar
  • 4,443
0 votes
1 answer
56 views

Is it possible to document with JSDoc a sort of type guard for document.getElementById?

The idea here is to generalize the following pattern: const element = document.getElementById('foo') if (!element || !(element instanceof HTMLInputElement)) throw new Error('Failed to locate HTML ...
Ben's user avatar
  • 1,361
1 vote
1 answer
44 views

Why isn't type guard applied in arguments of callback functions? [duplicate]

The following is a simple TypeScript code example. // Example Setup declare var foo:{bar?: {baz: string}}; function immediate(callback: () => void) { callback(); } // Type Guard if (foo.bar) { ...
Blue A's user avatar
  • 333
0 votes
1 answer
51 views

Limit argument types for guard function

How to implement a guard function isUndefined that only accept arguments whose type includes undefined? The function should assert that the argument is undefined. Basic implementation: function ...
Pavel's user avatar
  • 3
-1 votes
1 answer
51 views

Assert is typeguard

Can anybody explain to me why in the console log the notUser has a type const notUser: { items: any[]; } & User I expect it to be User interface User { id: string } function assVal (obj:any)...
programmer's user avatar
1 vote
1 answer
167 views

Why TypeScript type guard not narrowing down the union?

In the following TypeScript code, I expect contact to be narrowed down to GroupContact due to the type guard in place, but contact is still Contact | GroupContact. Why? type Contact = { id: string;...
Misha Moroshko's user avatar
0 votes
1 answer
25 views

How to properly find array element and cast when array has union types and generics?

I have the following type declarations: types.ts: type ItemKind = 'A' | 'B'; type ValidItem<TItemKind extends ItemKind = ItemKind> = { readonly type: TItemKind; readonly id: number; }; ...
Tristian's user avatar
  • 3,452
1 vote
1 answer
127 views

How to create a TypeGuard that mimics isinstance

I have to check an object that may have been created by an API. When I try using isinstance(obj, MyClass) it get a TypeError if obj was created by the API. I wrote a custom function to handle this. ...
Amour Spirit's user avatar
3 votes
0 answers
162 views

Python generic type annotation for pandas series

I am encountering a type checking error when running my code through typeguard. I the following file in my package: from __future__ import annotations import pandas as pd def col_sum(x: pd.Series[...
Michael Barrowman's user avatar

15 30 50 per page
1
2 3 4 5
16