英文:
Typescript connect primitive generic type with typeof method returns derived type
问题
以下是翻译好的部分:
我有一个带有值的类。该值可能是字符串或数字类型。根据值的类型,我想将不同的值解析为相同的类型。
因此,如果值是字符串类型,我想将给定的值解析为字符串。
这段代码可以工作,但我的类型定义可能有问题。是否有一种方式可以让编译器显示typeOfValue
的类型为"string"?
以下是我的当前解决方案。我知道在typeOfValue
方法中存在类型定义的问题:
class MyClass<T extends string | number> {
public constructor(private value: T) {
}
public typeOfValue(): `${T}` {
return typeof this.value as `${T}`;
}
}
const foo = new MyClass('foo');
// 我希望“theType”的类型为'string',但它的类型为'foo'
const theType = foo.typeOfValue();
英文:
I have a class with a value. The value might be of type string or number. Depending on the type of the value, I want to parse a different value to the same type.
So if value is of type string. I want to parse a given value to string.
The code works, but my typings are wrong. Is there a way that the compiler shows that typeOfValue
is "string"?
Here is my current solution. I know that there is an issue with my typing in the typeOfValue
method:
class MyClass<T extends string | number> {
public constructor(private value: T) {
}
public typeOfValue(): `${T}` {
return typeof this.value as `${T}`;
}
}
const foo = new MyClass('foo');
// I want "theType" to be of type 'string' but it is of type 'foo'
const theType = foo.typeOfValue();
I understand that T is 'foo' because the type 'foo' extends the type 'string'. So typescript is setting T to the type 'foo'. But at this point I want the base type 'string'.
答案1
得分: 1
如果您有一个类型来模仿 `typeof` 操作符:
```ts
type TypeOf<T> =
T extends Function ? "function" :
T extends object | null ? "object" :
T extends string ? "string" :
T extends number ? "number" :
T extends boolean ? "boolean" :
T extends undefined ? "undefined" :
T extends symbol ? "symbol" :
T extends bigint ? "bigint" :
never;
然后,您只需断言返回类型为 TypeOf<T>
class MyClass<T extends string | number> {
public constructor(private value: T) {
}
public typeOfValue() {
return typeof this.value as TypeOf<T>;
}
}
并且它将按预期工作:
const foo = new MyClass('foo');
const theType = foo.typeOfValue();
// ^? "string"
<details>
<summary>英文:</summary>
If you have a type to mimic the `typeof` operator:
```ts
type TypeOf<T> =
T extends Function ? "function" :
T extends object | null ? "object" :
T extends string ? "string" :
T extends number ? "number" :
T extends boolean ? "boolean" :
T extends undefined ? "undefined" :
T extends symbol ? "symbol" :
T extends bigint ? "bigint" :
never;
then you can just assert that the return type is TypeOf<T>
class MyClass<T extends string | number> {
public constructor(private value: T) {
}
public typeOfValue() {
return typeof this.value as TypeOf<T>;
}
}
and it'll work as expected:
const foo = new MyClass('foo');
const theType = foo.typeOfValue();
// ^? "string"
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论