question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. ItΒ collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Question: Is there a simple way to get a type reduced to only primitives?

See original GitHub issue

For example, if I have a type that is:

type Company = {
  name: string
  employees: Person[]
}

type Person = {
  name: string
  age: number
}

How can I get the deeply interpreted type, so that in Company instead of a reference to another type alias I get the properties of Person in there.

Issue Analytics

  • State:open
  • Created 2 years ago
  • Reactions:1
  • Comments:6 (1 by maintainers)

github_iconTop GitHub Comments

3reactions
zaripychcommented, Oct 27, 2021

I’m currently solving exactly same problem and I wrote a little script for it. My use case is to build a function that makes it more explicit which API’s a given shared type affects - when changed. So if you changed a type - you will see all the public boundary interface functions/api this change affects because it would lead to a jest snapshot change.

E.g. for this type:

type TestType = {
	optionalPrimitive?: string;
	nested: NestedType;
	get getterProp(): number;
	pick: Pick<PickAndChooseSource, 'first' | 'fourth'>;
	omit?: Omit<PickAndChooseSource, 'first' | 'fourth'>;

	functionCall(): void;
	callWithPromise: () => Promise<void>;
	callWithParams(param: { first: string; second: number }): number;
	callWithParams2(
		param: { first: string; second: number },
		deps: { properties: boolean },
	): number;

	num: number;
};

it prints:

{β€ˆ
  optionalPrimitive?: undefined | string;β€ˆ
  nested: {β€ˆ
    stringLiterals: 'success' | 'failed';β€ˆ
    simpleArray: string[];β€ˆ
    complexArray?: undefined | Array<β€ˆ
      {β€ˆ
        key: 'string';β€ˆ
        value: number;β€ˆ
      }β€ˆ
    >;β€ˆ
    intersection: {β€ˆ
      ok: true;β€ˆ
    } & {β€ˆ
      ok: false;β€ˆ
    };β€ˆ
    tuple: [β€ˆ
      number,β€ˆ
      string,β€ˆ
      {β€ˆ
        str: string;β€ˆ
      }β€ˆ
    ];β€ˆ
  };β€ˆ
  getterProp: number;β€ˆ
  pick: {β€ˆ
    first: number;β€ˆ
    fourth: null | {β€ˆ
      scissor: string;β€ˆ
      paper: string;β€ˆ
      rock: string;β€ˆ
    };β€ˆ
  };β€ˆ
  omit?: undefined | {β€ˆ
    second: string;β€ˆ
    third: null;β€ˆ
  };β€ˆ
  functionCall(): voidβ€ˆ
  callWithPromise(): Promise<void>β€ˆ
  callWithParams(param: {β€ˆ
    first: string;β€ˆ
    second: number;β€ˆ
  }): numberβ€ˆ
  callWithParams2(param: {β€ˆ
    first: string;β€ˆ
    second: number;β€ˆ
  }, deps: {β€ˆ
    properties: boolean;β€ˆ
  }): numberβ€ˆ
  value: {β€ˆ
    str: string;β€ˆ
    num: number;β€ˆ
  };β€ˆ
}β€ˆ

This is my first attempt at this and the first time I’m using ts-morph - I welcome any feedback πŸ‘ https://gist.github.com/zaripych/963fa6584524e5b446b70548dbabbf65

I’m almost there, but currently have a problem for properties that do not have declared types. E.g. getDeclarations() returns an empty array. 🀯 Does anybody know if there is an additional way to get a property type?

1reaction
DaniGuardiolacommented, Sep 18, 2022

@zaripych you just helped me fix a problem in which types created with Omit where missing keys. I fixed it by doing getTypeAtLocation, just like you. Thanks a lot!

Read more comments on GitHub >

github_iconTop Results From Across the Web

Primitive Types - Wikibooks, open books for an open world
All the primitive types have a fixed size. Thus, the primitive types are limited to a range of values. A smaller primitive type...
Read more >
Is there a way to create nominal types in TypeScript that ...
I would like to represent these variables with the basic number primitive, but disallow assignment of a longitude to a latitude variable in...
Read more >
Data Types in Java | Primitive and Non-Primitive Data Types
This article on Data Types in Java will give you a brief insight into various primitive and non primitive data types in Java...
Read more >
Primitive Obsession - Refactoring.Guru
Even better, move the behavior associated with this data into the class too. For this task, try Replace Data Value with Object.
Read more >
When Primitives Behave Like Objects - KIRUPA
Strings Aren't the Only Problem. Because of how fun and playful they are (kind of like a Golden Retriever!), it's easy to pick...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found