简体   繁体   English

Typescript 在参数中强制使用别名类型

[英]Typescript enforce alias type in argument

I am currently having an issue with type unions/aliases.我目前遇到类型联合/别名的问题。 I have an alias for values which might be null or undefined and a function that handles these values.我有一个值的别名,它可能是 null 或 undefined 以及一个处理这些值的 function。

This works fine and everything is safe.这工作正常,一切都是安全的。 No there are situation where through oversights or whatever, a safe value, which can't be null or undefined is passed to the function.不存在通过疏忽或其他任何情况,不能是 null 或未定义的安全值被传递给 function。 This does not break anything, because the function expects safe values aswell, but Typescript does not complain.这不会破坏任何东西,因为 function 也需要安全值,但 Typescript 不会抱怨。

The code looks something like this:代码看起来像这样:

type Maybe<T> = T | null | undefined;

function handleDangerousValue<T>(val: Maybe<T>): T {
    // Check stuff
    return val as unknown as T;
}

const safeValue: number = 1;
const unsafeValue: Maybe<number> = null;

// Would like a type error here
handleDangerousValue(safeValue);

// This is fine
handleDangerousValue(unsafeValue);

I would like to somehow have Typescript tell me that safeValue does not have the correct type.我想以某种方式让 Typescript 告诉我safeValue没有正确的类型。

I know that this does not work because of how the union type is defined, but I do not know any solution on how to enforce this.我知道这不起作用,因为联合类型是如何定义的,但我不知道如何执行此操作的任何解决方案。 This would probably be solvable by having DangerousValue<T> become a class, but I would prefer it as a type, as I do not want extra boilerplate to solve a typing problem.这可能可以通过将DangerousValue<T>变为 class 来解决,但我更喜欢它作为一种类型,因为我不希望额外的样板来解决打字问题。

edit:编辑:

I tinkered a bit more and got some progress:我修修补补了一下,并取得了一些进展:

type Maybe<T> = T | null | undefined;
type EnforceMaybe<T> = Maybe<T> extends T ? Maybe<T> : never;

function handleDangerousValue<T>(val: EnforceMaybe<T>): T {
    // Check stuff
    return val as unknown as T;
}

const safeValue: number = 1;
const unsafeValue: Maybe<number> = null as Maybe<number>;

// Correctly finds issue
const n1: number = handleDangerousValue(safeValue);

// Now thinks that the return type should be a Maybe
const n2: number = handleDangerousValue(unsafeValue);

Now the values that can't be null are not allowed, but typescript is not able to infer the inner type of the Maybe anymore.现在不能为 null 的值是不允许的,但是 typescript 无法再推断出Maybe的内部类型。

Can I somehow infer that for the return type?我可以以某种方式推断返回类型吗?

Solved it by casting the return type:通过转换返回类型来解决它:

type Maybe<T> = T | null | undefined;
type EnforceMaybe<T> = Maybe<T> extends T ? Maybe<T> : never;

function handleDangerousValue<R extends NonNullable<T>, T>(val: EnforceMaybe<T>): R {
    // Check stuff
    return val as unknown as R;
}

const safeValue: number = 1;
const unsafeValue: Maybe<number> = null as Maybe<number>;

// Correctly finds issue
const n1: number = handleDangerousValue(safeValue);

// Now thinks that the return type should be a Maybe
const n2: number = handleDangerousValue(unsafeValue);

I know that the value will not be null when returning, therefore it should be fine to cast it.知道返回时该值不会是 null,因此可以转换它。 For casting I created a second generice which gets the original type out of the Maybe type.对于铸造,我创建了第二个泛型,它从Maybe类型中获取原始类型。

Now I can't pass non-nullable values to the function but will get the correct return type if one is passed.现在我无法将不可为空的值传递给 function 但如果传递了一个值,将获得正确的返回类型。

Playground Link 游乐场链接

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM