如何在类型T和U上声明泛型函数,不允许T和U的空交集

时间:2018-10-28 20:53:50

标签: typescript generics

给出界面:

interface Extendable<T> {
    extend<U>(q: U): T & U
}

当T和U的交点为空时,您将如何修改它以便出现编译错误?

T & U === never

在我的用例中,类型T和U是特定的字符串值或它们的并集。

下面是如何使用该接口的示例:

type S = 'Select'
type U = 'Update'
type D = 'Delete'
type I = 'Insert'
type V = 'Values'
type SUDI = S | U | D | I
type SUD = S | U | D
type SV = S | V

declare let sud: SUD, s: S, i: I;
declare let root: Extendable<SUDI>
declare let where: Extendable<SUD>

root.extend(sud)
// return type (S | U | D | I) & (S | U | D) = (S | U | D)

where.extend(i)
// return type (S | U | D) & (I) = never
// can this be a compile error instead?

2 个答案:

答案 0 :(得分:1)

您要表达的关系实际上只是一个扩展关系。如果您有工会,则工会成员的一部分将扩展原始工会

interface Extendable<T> {
    extend<U extends T>(q: U): T & U
}

type S = 'Select'
type U = 'Update'
type D = 'Delete'
type I = 'Insert'
type SUDI = S | U | D | I
type SUD = S | U | D

declare let sud: SUD, s: S, i: I;
declare let root: Extendable<SUDI>
declare let where: Extendable<SUD>

root.extend(sud) //ok
where.extend(i) // I !extends SUD

修改

如果两个联合之间没有公共成员,我们可以使用条件类型来触发错误。由于该关系不是传统的关系,因此该错误也不会是传统的编译器错误。如果不满足条件,我们将额外的字符串文字类型添加到参数中,这将触发错误。

type ValidateCommonunionMemebers<T, U, TErr> = [T] extends [U] ? {} :
    [U] extends [T] ? {} : TErr;

interface Extendable<T> {
    extend<U>(q: U & ValidateCommonunionMemebers<T, U, "T and U should have some common memebers">): T & U
}

type S = 'Select'
type U = 'Update'
type D = 'Delete'
type I = 'Insert'
type V = 'Values'
type SUDI = S | U | D | I
type SUD = S | U | D
type SV = S | V

declare let sud: SUD, s: S, i: I, sudi:SUDI;
declare let root: Extendable<SUDI>
declare let where: Extendable<SUD>

root.extend(sud)
// return type (S | U | D | I) & (S | U | D) = (S | U | D)

where.extend(i) // Error Type '"Insert"' is not assignable to type '"T and U should have some common memebers"'.
where.extend(sudi) 

答案 1 :(得分:0)

  

当T和U的交点为空时,您将如何修改它以便出现编译错误?即 <div class="jumbotron" id="skills"> <h1>Check Out My Design Work: <a href="https://www.slideshare.net/AmyWhite95"><img src='images/slideshow.jpg' height="100px" width="100px" /></a></h1> <br /> <!--Trigger Modal--> <div class="container"> <div class="row"> <div class="column"> <div class="Incontent"> <img class="portfolioImages" src="images/Cards1.JPG" alt="Card Design" style="width:100%" height="500px"> <h3>Card Design</h3> <p>This was for a project in Typography I. The goal was to design and print a deck of cards along with a box.</p> </div> </div> <div class="column"> <div class="Incontent"> <img class="portfolioImages" src="images/FrontPageNewsWhite.jpg" alt="Newspaper Design" style="width:100%" height="500px"> <h3>Newspaper Design</h3> <p>This was a project for Media Design. The goal was to design the front page of a magazine with good headlines and layout.</p> </div> </div> <div class="column"> <div class="Incontent"> <img class="portfolioImages" src="images/WhiteAInfographic.jpg" alt="Infographic Design" style="width:100%" height="500px"> <h3>Infographic</h3> <p>This was a project for Media Design. The goal was to design a well titled and laid out infographic.</p> </div> </div> <div class="column"> <div class="Incontent"> <img class="portfolioImages" src="images/vectorgirlJPG.jpg" alt="Vector Design" style="width:100%" height="500px"> <h3>Vector Design</h3> <p>This was a project for Computer Graphics I. The goal was to vectorize a portrait with Adobe Illustrator.</p> </div> </div> </div> </div> </div> <div class="container"> <div class="row"> <div class="column"> <div class="Outcontent"> <img class="portfolioImages" src="images/WhiteAWebsite.jpg" alt="Website Design" style="width:100%" height="500px"> <h3>Website Design</h3> <p>This was for a project in Media Design. The goal was to re-design a bad website.</p> </div> </div> <div class="column"> <div class="Outcontent"> <img class="portfolioImages" src="images/SpecSheet1.JPG" alt="Spec Sheet Front" style="width:100%" height="500px"> <h3>Spec Sheet (Front)</h3> <p>This was a project for Typography I. The goal was to design a spec sheet with provided material.</p> </div> </div> <div class="column"> <div class="Outcontent"> <img class="portfolioImages" src="images/SpecSheet2.JPG" alt="Spec Sheet Design Back" style="width:100%" height="500px"> <h3>Spec Sheet (Back)</h3> <p>This was a project for Typography I. The goal was to design a spec sheet with provided material.</p> </div> </div> <div class="column"> <div class="Outcontent"> <img class="portfolioImages" src="images/TypeForm.JPG" alt="Type Form Design" style="width:100%" height="500px"> <h3>Type Form Design</h3> <p>This was a project for Typography I. The goal was to create a design using only our initials.</p> </div> </div> </div> </div> <!-- The Modal --> <div id="myModal" class="modal"> <span class="close">&times;</span> <img class="modal-content" id="img01"> <div id="caption"></div> </div>

除非T & U === neverT & U都不是,否则

never永远不会是T

您不能将U用作泛型的占位符,因为它可以分配给任何对象,例如

never