Linked Questions
17 questions linked to/from Is there a way to "extract" the type of TypeScript interface property?
0
votes
2
answers
861
views
Can I extend part of an Interface in Typescript? [duplicate]
Lets say I have an interface:
// I get this from an external library so I have no control over it
interface Balloon {
diameter: number;
color: "red" | "blue" | "green"...
-1
votes
1
answer
274
views
Extract the type of a property from an interface [duplicate]
How can I reuse the type of someMethod ?
Interface Foo {
someMethod: // want to reuse this type without copy pasting
}
const myValue: // I want it to be of type Foo.someMethod
0
votes
1
answer
166
views
How to create a type that refer to a type of an interface's field? [duplicate]
How to create a type that refer to a type of an interface's fields?
Here is what I want to achieve:
interface SomeOption {
readonly name: string;
readonly value: number;
}
type OptionName = ...
0
votes
1
answer
107
views
How to get an interface's properties from typescript [duplicate]
I want to get a "postType" Property from an interface defined like this
export interface PostType {
postType: {
title?: string;
content?: string;
};
}
The purpose I want to take out and ...
0
votes
0
answers
72
views
TypeScript infer specific property type from object interface [duplicate]
Say, I have this interface:
interface MyObject {
all: string[] | {[value: string]: string}
some: string[]
}
In my code I have an object of this type which I deconstruct, and then use one of the ...
0
votes
0
answers
45
views
How to extract type of specific key from interface [duplicate]
Given an interface like this:
interface Person {
name: Some | Pretty | Complex | Type;
age: number;
}
I'd like to get the type of a specific key, e.g. name. So I could e.g. write a function like (...
0
votes
0
answers
30
views
How do I extract the type of a particular property in a given type? [duplicate]
So let's say I have a type Foo defined as:
type Foo = {
bar: string;
xxx: {
abc: string;
def: number;
};
};
How do I extract the type of my property xxx to define let's ...
328
votes
10
answers
677k
views
How to get a variable type in Typescript?
I have a variable.
abc:number|string;
How can I check its type? I want to do something like below:
if (abc.type === "number") {
// do something
}
17
votes
2
answers
8k
views
Typescript: declare variable with same type as another
Is there any way to declare a variable with the type of another variable? For instance, I declare a class member with some type, and then later I want to declare another variable in a function of the ...
0
votes
1
answer
3k
views
Get type of an interface's property in TypeScript
I want to extract a type from my an interface by it's property name to reuse in a Record. I am having difficulties with syntax to retrieve the type by property name. I am trying to make it a bit more ...
0
votes
2
answers
633
views
Typescript : how to do cast to an interface property?
How to cast geometryType as I get this error :
IShape is a Type not a Namespace
for
interface IShape {
readonly geometryType: "RECTANGLE" | "SQUARE"
}
let geometryType =...
3
votes
0
answers
478
views
How do I pass Pick as a key inside Record type?
Let's say I have an interface
interface Test {
a: string;
b: string;
c: 'x' | 'y' | 'z';
}
How do I construct an object type which requires all the keys of the c union type?
Record<...
2
votes
1
answer
195
views
Accessing static properties from within an interface declaration
class A {
public static readonly TYPE = "A";
}
interface forA {
for: A.TYPE
}
As you can see, I'm trying to access A.TYPE from forA, so that I can do a form of type guarding.
But, I'm ...
2
votes
1
answer
206
views
How to extract nested type of an attribute passed as props to a React function component
If I have an interface defined for a function component, say
MyFuncComp.tsx
import React from 'react'
interface MyFuncCompProps {
x: string
y: number
z: Array<ComplexType>
}
export ...
0
votes
1
answer
73
views
Get the return type from a method parameter
I would like to get the of the return type from a method where I pass the key as a parameter and the return is type from the interface.
I looked at this answer Is there a way to "extract" ...