Closed
Description
openedon Nov 1, 2018
I have only done TS for about a 3/4 of a month, and JS for less than 2 months, so maybe I'm just doing someting fundamentally wrong, but after questioning people that have done this for some time I think these are some compiler-issues.
On an somewhat related note, if any of you managed to implement the 'Basic example' from here in TS without errors or using //@ts-ignore please let me know. Also why is 'Proxy' in the ES2015 lib and not in the 'ES6' lib, and whats the difference between these libs?
TypeScript Version: 3.1.5 and 3.2.0-dev.20181101
Search Terms: generic, extend, extended, base-type, basetype, type
Code
// ------------------- with generic type -------------------//
const extGenTypeFactory = <TObj extends Object>(specialObj: TObj) => {
/**
* Throws 'TS2312: An interface may only extend a class or another interface.'.
* Doesnt compile the stuff using the interfaces and themself
*/
interface TExtObj extends TObj {
newAttr: any
}
// works
let newObj = specialObj;
let extObj: TExtObj
// This particular issue is Solved in 3.2, keeping it for understanding of the comment line
// TS2698: 'Spread types may only be created from object types.'.
extObj = {
...specialObj,
newAttr: "some other"
}
return extObj
}
// Tried to evade problem with suggestion from 'Why doesn't type inference work on this interface: interface Foo<T> { } ?'
const obj = extGenTypeFactory({ attr: "take that" })
// works fine
const genTypeFactory= <TObj>(specialObj: TObj) => {
return specialObj;
}
// \------------------- with generic type -------------------//
// ------------------- with known type -------------------//
interface TObj extends Object {
hasAttr: any
}
/**
* Works fine.
*/
interface TExtObj extends TObj {
newAttr: any
}
// \------------------- with known type -------------------//
Expected behavior:
Compiles without error
Actual behavior:
Throws TS2312 and TS2698
Playground Link:
Related Issues:
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment